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

Getting icon of “modern” Windows app from a desktop application?
How prevent Controls binding to Panel
Can a Base Class Method return the type of the derived class?
Accessing an instance object within an instance object
Deserializing an empty array using Json.NET
Opening a protected workbook with spreadsheetgear
Unreal Engine 4 Setup problems
Conversion date when storing in a table
Why is the surrounding type of a dynamic expression not statically resolved in C#?
webkit.net error “Cookies not enabled in browser”
Unable to Deserialize XML within C#
Creating a new group and setting permissions in Active Directory using C#
selecting data from dropdown without refreshing the page
Select first letter of name with linq query from database
TPL Dataflow is either Parallel or Ordered but not both
What is the replacement of Multitrigger in Windwos phone 8.1

Categories

HOME
reactjs
teradata
typo3
c++builder
data-modeling
sms
minimax
runtime
brightway
amazon-data-pipeline
cucumberjs
alert
aws-devicefarm
android-wifi
datazen-server
smali
fastboot
ng-tags-input
connection-pooling
jira-zephyr
knapsack-problem
restangular
pypy
akamai
webspeech-api
gstreamer-1.0
const
pygraphviz
cowboy
irr
gridgain
html-entities
qtwebkit
ionic2-select
spring-cloud-bus
dcom
nsalert
robotc
android-music-player
instaparse
cherry-pick
razorengine
archiva
wonderware
php-gd
traminer
consul-template
eggplant
pdcurses
esb-toolkit-2.1
gcc-warning
httpmodule
screen-lock
nfa
optimistic-locking
alpha-beta-pruning
iphone-6
method-overloading
selenium-grid2
livescript
drawable
winrun4j
cvi
html5-filesystem
windows-phone-store
mixradio
carrier
nomachine
infomaker
static-class
wpd
select2-rails
pudb
mysql2
msbuild-task
ftpwebrequest
webpage-screenshot
towerjs
p4.net
ironpython-studio

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App