c#


Build JObject from JSONPath


I'm a bit new to using the Newtonsoft JSON library for .NET. Is there any way to create a JObject or JToken from a JSONPath?
So for example something like the following.
string jsonPath = "$.ArrayA[0].ArrayB[0].Property";
JObject jObj = JObject.FromJSONPath(jsonPath); // SOMETHING LIKE THIS
The result would be a JObject or JToken that looks like this.
{
"ArrayA": [{
"ArrayB": [{
"Property": ""
}]
}
}
No.
If you have some existing JSON, you can parse it to a JToken and then select one or more descendant JTokens from it using SelectToken or SelectTokens with a JsonPath expression. For example:
string json = #"{ ""ArrayA"": [{ ""ArrayB"": [{ ""Property"": ""foo"" }] }] }";
JToken token = JToken.Parse(json);
JToken fooToken = token.SelectToken("$..Property");
Console.WriteLine(fooToken.ToString()); // prints "foo"
You can also manually build a nested structure of JTokens. For example, you can create the JObject in your question like this:
var obj = new JObject(new JProperty("ArrayA", new JArray(
new JObject(new JProperty("ArrayB", new JArray(
new JObject(new JProperty("Property", ""))))))));
However, there is no built-in way to create a JToken from nothing but a JsonPath expression. You would need to roll your own method to do something like that. But keep in mind that JsonPath was designed as a query mechanism; it doesn't map cleanly to creation of new objects. Here are some issues you would need to think about:
In your example expression, $.ArrayA[0].ArrayB[0].Property, what type is Property? Is it string, number, boolean, object or an empty array? How would you specify that?
How would you specify creation of an object with multiple properties?
What would an expression like $..book[(#.length-1)] create?

Related Links

How would you invoke an event with reflection?
Conditional Compile of Generic Methods
VS WinForm designer always changes custom constructor?
Run C# application from cmd
Knowing the status of a very long function (execution time) in another class [closed]
Add a single row containing text taking up entire area of WPF Datagrid
CGBitmapContext in MonoTouch throws invalid handle exception
Create a list to display the contents of a dictionary?
Get file to FileStream from remote path with another user credentials
Implementing the min & max function with group by and having in linq
JSON or HTML returned from webmethod
BackgroundWorker doesn't handle exception
Implementing C++/clr's auto_handle functionality in C#
Get Children from ListBox can't get children that haven't been viewed
DelegatingHandler for response in WebApi
MVC4 bundling CSS failed Unexpected token, found '#import'

Categories

HOME
excel-formula
optimization
oracle-sqldeveloper
dronekit
latex
liquibase
glsl
sms
confluence-rest-api
comments
markdown
elastalert
chef-recipe
box2d
internet-explorer-7
wallet
novacode-docx
gomobile
lotusscript
fastboot
ng-tags-input
pushbullet
uipath
ecmascript-2017
oracle10g
nsopenpanel
jopendocument
pydub
visual-studio-community
custom-fields
emr
kundera
ipython-parallel
ios-autolayout
webclient
mkdir
flex3
pygraphviz
idea-plugin
mixed-models
socketcluster
python-venv
bootstrap-carousel
easyquery
react.rb
emacs24
openidm
runtime-permissions
perldoc
gige-sdk
node-java
aot
hellosign
cc
grib
blackberry
chain
nls
copy-constructor
portaudio
alarmmanager
timefield
ownership
gevent
optional-parameters
networkstream
qtruby
db2-zos
weblogic-maven-plugin
avaudiorecorder
overlapping
wiql
exchange-server-2007
litedb
photogrammetry
esb-toolkit-2.1
douglas-peucker
chunks
jsonschema2pojo
powerpoint-2010
redmine-plugins
switchpreference
amzi-prolog
behance-api
cs193p
android-relativelayout
object-oriented-database
db2-connect
xtify
pechkin
infinity.js
zepto
freeimage
gssapi
report-viewer2010
waveout
aspmenu-control
commonsware
datejs
overlays
p4.net
webdb
focus-stealing

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App