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

Sort List using Filename
Unable to restore a nuget of .net core class lib into another
WPF camera animation issue
Unexpected results with HashSet.Contains and custom IEqualityComparer
Input field which is not visible to user
Xamarin.Auth Android project results in “Cannot find symbol” error for CustomTabs?
Why can't I get all ObjectCreationExpressionSyntax if initialized as null?
SqlDateTime overflow on join. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
Create a count loop c#
NewtonSoft.Json error of version?
XUnit Test different results under netcoreapp1.1 and net462
how to get Identity Value for each iteration bulk insert
Set value to input type=“date” from codebehind
Objects changing on inspection while debugging
Prism + Xamarin.Forms Project error after closing app on Android?
Switching Tabs and Windows are not shown during execution of test using Selenium 2.53.1

Categories

HOME
classification
msbuild
pelican
dynamic
reference
ebay-api
watson-iot
data-modeling
crm
bitbucket
symfony-sonata
ebay
autocad
comments
zabbix
rethinkdb
vsixmanifest
promotions
subprocess
openbsd
angular-universal
vichuploaderbundle
virtualenv
oxid
webseal
appsettings
jsfiddle
ringcentral
virtuemart
passbook
gpo
xna
ppp
aspxgridview
proof-general
spock
restangular
hierarchy
boost-log
blockly
powerbi-embedded
sampling
multi-user
rtsp
jibx
lex
nsdateformatter
tapply
contenteditable
ocpjp
azure-data-catalog
spring-webflow-2
evaluation
openidm
visio-2010
sharpdevelop
boost-regex
multi-select
bipartite
timefield
ownership
cedar-bdd
gevent
android-tabs
zsh-completion
scriptengine
nsoperationqueue
angular-i18n
usps
radar-chart
nslocalizedstring
dynamics-nav-2013
matlab-engine
skscene
random-sample
updating
callfire
httpmodule
nexusdb
quickfixn
pysvn
grunt-contrib-connect
apache-pivot
novell
go-gin
powerpoint-2010
agent-based-modeling
livescript
twitter-finagle
winrun4j
pidgin
new-operator
iwork
internet-radio
grunt-wiredep
randomaccessfile
typography
nsautoreleasepool
cron4j
linear-interpolation
openafs
ruby-1.9
code-organization
vectorwise
youtube.net-api
zipstream
arraycollection
nscharacterset
chromebug
carbon-emacs

Resources

Encrypt Message