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

Merging touching polygons giving wrong result
Getting users from active directory using openldap c#
How to allow only one checked box in the gridview?
Null Reference Exception being thrown when using a data template selector in a user control WPF
Synchronous Task Execution in C#
Having to click button twice to trigger postback in asp.net
How to execute a method after completion of three proccesses in .net
Parallelize transitive reduction
Why does Visual Studio create double closing brackets for C#6 interpolated strings?
Why does XNA upgrade VS 2010 from v. 10 to v. 11?
Download All Blobs from single Azure Container
Telerik HTML5 Report Viewer - Error loading the report viewer's templates
Extracting image from a slide of a .pptx
How to sort in EF parents by number of children of children of children?
Fetch Data from database to Fullcalendar.io using Json
How can I handle button Click in XAML?

Categories

HOME
max
algolia
kendo-ui-angular2
azure-documentdb
azure-active-directory
botframework
primefaces
windows-server-2008
opencv4android
x86-64
hex
crm
pandoc
xss
terminal
sugarcrm
browser-cache
pip
ephesoft
angularjs-ng-repeat
coding-style
activejdbc
jpql
aws-sdk-cpp
decoding
mongodb-replica-set
oxid
android-notifications
web-testing
game-engine
powerpivot
vivado-hls
ibatis
ampscript
gomobile
descriptor
ajaxcontroltoolkit
ejb-3.1
serenity-js
fibonacci
beaker-notebook
inline-assembly
oracle-golden-gate
windows-mobile
dynamic-memory-allocation
uibarbuttonitem
bytebuffer
pushpad
semantic-ui-react
akamai
epub
rtos
android-geofence
gstreamer-1.0
anypoint-studio
const
area
armadillo
keychain
pyexcel
php-mongodb
binary-decision-diagram
peerjs
result
appcmd
selection-sort
wicketstuff
cardview
vertex-shader
sqlproj
stringbuilder
strtol
virtualenvwrapper
objectify
database-administration
phpdesigner
archiva
mod
web-optimization
video-embedding
markerspiderfier
imageicon
database-project
jts
android-togglebutton
bootstrap-tabs
avrcp
tidesdk
gcc-warning
radgrid
gmaps4jsf
autopep8
xmla
backtrace
indices
go-gin
parsefloat
windows-search
indic
ccmenuitem
msdropdown
visual-studio-setup-proje
git-subtree
carrier
nativequery
xml-validation
thucydides
cab
static-class
angularjs-select2
symbian3
qmainwindow
algol
moonscript
linear-interpolation
xcode4.6.3
dancer
erlide
project-files
copyfile
zend-validate
sipdroid
lightopenid
moss2007-security
xslt-grouping
helios
coderush-xpress
file-icons

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