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

cannot select the item in the dropdownlist, if the list has only one item
Getting errror in UDP connectiion
Internet Explorer opens a new pop -up window on window.close() and then closes the newly opened popup
Showing horizontal bar for datagridview for overflowing cell content
How to assign HTML content to iframe control from asp.net codebehind?
ASMX Web Service receive large amount of data
Connect C# to Mysql database
How to get value of Oracle DATE column in C# code that matches SQL Developer's
Mimicing Inheritance in Protobuf-net v2
Add a 2nd Series to an Excel chart using C#
How do I break a string into an array (or List) in C#?
Count values in Dictionary using LINQ and LINQ extensions
IOException in StreamWriter
HTML agility pack - removing unwanted tags without removing content?
Not to able to navigate to the next page using facebook id/pswd on IE and Windows phone Browser
C# datetime parse issue

Categories

HOME
azure-documentdb
kendo-ui
azure-active-directory
dynamic
nagios
boxplot
cryengine
datetimepicker
artifactory
travis-ci-cli
zabbix
genexus
webpack-dev-server
spring-data-solr
osclass
github-flavored-markdown
console-application
luis
greensock
facebook-marketing-api
conditional-formatting
spring-webflow
url-parameters
descriptor
include-path
klipfolio
google-drive-android-api
functional-dependencies
pixels
datanucleus
large-files
sqldependency
delphi-2010
jboss-arquillian
visual-prolog
kofax
lighting
strategy-pattern
kepserverex
mapquest
pygraphviz
google-cloud-powershell
logan-square
angular-fullstack
line-endings
anonymous-function
qt3d
cedar-bdd
settext
rowname
sprockets
jde
window.location
yfiles
gulp-cdnizer
identifying
traminer
uinavigationitem
joomla3.3
tokudb
pdcurses
page-refresh
stderr
copy-on-write
cl.exe
mathics
chunks
kuka-krl
adabas
cab
static-class
wpa
gwt2
system.transactions
gssapi
databus
datasheet
parsec
joomla1.7
service-layer
hs-err
chromebug
jquery-attributes
newspeak
data-formats
paperless





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