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

C# faculty : weird results [duplicate]
Which MVC version to start with [closed]
What is “88-type” in C# and how can i use it?
C# Regex to convert yyyyMMddhhmm
EntityFramework package version=“6.1.3” and Web config version 6.0.0.0?
How to read a binary formatted string from a file?
Why My combobox binding not working
convert timespan to datetime
call to method inside code behind file(aspx.cs file) from controller method
Casting exposes private data via getter method
How to get a proper date from a text?
Problems converting an Oracle plsql stored procedure call to C# (invalid array bind size)
How to handle null value in entity framework sum function
Audio stream requested using HttpClient doesn't play until file is completely downloaded
Complex grouping in Linq? C#.net
How to prevent duplicate items from being added to an IRepository?

Categories

HOME
skype-for-business
ssms
twitter-bootstrap
cors
pelican
popover
c++builder
skypedeveloper
watson-iot
lithium
powerquery
samsung-gear-s2
pyodbc
oms
angular-universal
email-attachments
tmux
jinja2
box2d
brightway
template10
cucumberjs
civicrm
monad-transformers
combinations
opengraph
concatenation
procedural-generation
poisson
scripting-language
angular-schema-form
cqlsh
mplab
node-soap
n1ql
multi-user
var
blueimp
wufoo
npoco
anonymous-function
jenkins-2
stream-socket-client
spring-cloud-bus
dcom
business-catalyst
bipartite
esprima
spring-social-twitter
mavlink
mongodb-3.3
class-design
xcode7.1
altova
php-java-bridge
audiorecord
aws-kms
mod
pebble-js
radar-chart
paypal-subscriptions
skbio
ooad
std
wordpress-plugin
nslocalizedstring
opendolphin
controlpanel
tokudb
vectordrawable
grails-3.0.9
radgrid
xcode5
drools-guvnor
pnunit
agent-based-modeling
indic
scjp
satisfiability
mongoid4
thucydides
ironworker
android-library
symbian3
outlook-object-model
optimistic-concurrency
wxperl
scriptmanager
automapper-2
xpolog
mismatch
method-call
fxcopcmd
lgpl
miniport
carbon-emacs
focus-stealing
pixel-bender
disk-based
office-2003

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