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

Error while generating views with with Mysql and EF6
Identifying specific field that fails a conversion during a Database Query
EF Core “Invalid column name 'Discriminator'” error with inheritance
Gridview post back event
Got Unhandled Exception :: System.Xml.XmlException: Text node cannot appear in this state
“Syntax error in FROM clause” using OleDb in C# but no error for same query in Access itself
Custom Effect on model instance makes BasicEffect unrecognizeable on other (separately loaded) instance
Issue with Xamarin App Connecting to WCF Service over https
Reactive Extensions / UI cache web request data
ListView in UWP has unexpected behavior when INotifyCollectionChanged reports bulk changes
Singleton: create instance inside static constructor [duplicate]
2d array calculator in C# [closed]
Set TextBlock's ToolTip content
An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code when creating a List
Non-Interface methods [closed]
NullReferenceException at PropertyChanged Event (Prism 6.3)

Categories

HOME
pact
classification
kendo-ui
azure-active-directory
virtual-machine
mysqli
file-upload
skypedeveloper
spotfire
magento-1.9.2.1
lithium
ms-application-insights
sdk
foreach
atlassian-fisheye
symfony-3.2
collision-detection
samsung-gear-s2
ephesoft
mamp
vichuploaderbundle
eclipse-orion
swift3.1
dompdf
openoffice-basic
keystone
jq
git-branch
lotusscript
rndis
rvm
functional-dependencies
chartist.js
typeclass
osgi-bundle
ds-5
motherboard
outlook-2010
aspose.pdf
icu
php-mongodb
paperclip
f#-fake
scrapinghub
servicebus
reqif
visio-2010
trusted-computing
lightspeed
google-scholar
recurring
weblogic-maven-plugin
encode
imageicon
identifying
dnvm
winforms-interop
xml-simple
playn
avd
mri
method-overloading
sync
triplestore
isl
winrun4j
webshim
code-smell
power-management
connections
facebook-likebox
mysql-error-1146
oc4j
jython-2.5
dynamic-expresso
latex-suite
codeigniter-form-helper
gwt2
brew
auctex
freeimage
expresso-store
netzke
wxperl
win-prolog
unordered-set
idtabs
nsundomanager
log-shipping
youtube.net-api
fxcopcmd
webdb
mysql-pconnect
user-preferences
unauthorized
managed-code
memory-consumption

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