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

Implementing Depth First Search into C# using List and Stack
javascript permission problems inside c# WebBrowser control
Display a form in a console application at a particular point(in the middle of execution)
Delete all items from listobox
scrolltop+clientY not working in Mozilla Firefox
Reading from multiple Text Files , arrays show on listbox and lables
C# - Get JavaScript variable value using HTMLAgilityPack
Customize c# WinForm application for multiple customers
Loading a form in another Thread in .Net 2.0?
MVC BaseController handling CRUD operations
Simple thread example needed
WCF with Unity, configuring the dependencies through code
Pass complex objects through javascript to a .Net Service
ThisAddin.Ribbon (Excel) is always null since installing VS2010 SP1
.NET setup project - Installing to a shared network drive
javascript Request.QueryString

Categories

HOME
ckan
display
ebay-api
where
azure-resource-manager
pandoc
opacity
liquidsoap
promotions
facebook-sharer
drush
chef-recipe
greensock
orange
aws-devicefarm
custom-component
google-drive-android-api
screen-scraping
aws-iot
v4l2
oracle10g
rollupjs
mongoose-im
adobe-captivate
large-files
pypy
foxpro
login-required
android-scrollview
webclient
angularjs-resource
android-databinding
scip
jibx
iterm2
motion
ape-phylo
mef
text-formatting
xmldom
stream-socket-client
flask-socketio
multivalue
dotnet-cli
sharpdevelop
sharpssh
r-commander
apache-commons-cli
git-push
xcode7.1
foreground
audiorecord
jsondoc
apache2-module
clear
javapackager
exchange-server-2007
musicbrainz
firewire
cakephp-2.6
minko
beagleboard
sony-xperia
lwuit
gnip
nsnotificationcenter
parsefloat
msdropdown
django-tests
dateadd
elixir-framework
starling-framework
db2-connect
mahara
svg-android
thucydides
google-mirror-api
reference-manual
netzke
systrace
jqtransform
quazip
excellibrary
activescaffold
ihtmldocument2
urlscan

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile