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

Preventing edit of PRIMARY KEY COLUMN value in C#
How to show CPU usage in a Chart c#
How we Redirect one Page to AnotherPage with Server.MapPath
Accessing a file through System.IO while a folder in the path is a shortcut
Resharper test runner throw a TypeLoadException
Entity framework use of Database Views
How to bind a String to a TexBlock Property ? WPF c# [closed]
SqlCommand - Select ISNULL
WPF MVVM Light: Command.RaiseCanExecuteChanged() doesn't work
Stop anchored controls from moving when resizing the form in the designer
SQL c# insert Command doesn't work
How to get keys and values from a dictionary
How to avoid downloading files to the download folder and download to other specific folder only in c#
Best way to handle Convert failures?
Divisors of triangle numbers (Euler 12)
Covariance issue with interfaces causes back and forth casting

Categories

HOME
url
swift3
cluster-analysis
highmaps
okhttp3
opc-ua
terminal
glympse
kohana
token
phpexcel
runtime
jpql
angular-universal
wowza
ndepend
comma
hql
sha1
facebook-marketing-api
vivado-hls
ofbiz
indri
loader
xna
functional-dependencies
bootstrap-accordion
symlink
android-cursorloader
ds-5
jql
ports
epub
odp.net
hippocms
rtsp
yelp
jibx
latency
cowboy
keychain
autodesk-data-management
apiman
text-extraction
intervention
azure-data-catalog
anonymous-function
openxava
jenkins-docker
seyren
blackberry
huawei
lattice
stringbuilder
alarmmanager
cherry-pick
kik
jde
trendline
maven-antrun-plugin
om
borland-c++
android-networking
data-representation
gcc-warning
maven-archetype
chunks
crtdbg.h
random-access
yosemite
lwuit
famous-engine
gnip
tigase
agent-based-modeling
drawable
parallel-port
yandex-api
jsmpp
git-subtree
kernel-density
carrier
xml-validation
myrrix
linqdatasource
algol
feincms
linear-interpolation
mail-gem
report-viewer2010
blackberry-eclipse-plugin
aspmenu-control
django-1.4
socketasynceventargs
visual-leak-detector
capitalization
session-hijacking
execjs
lightopenid
object-tag
hwnd
nbehave
netbeans7.0
qprocess
time-management
asp.net-1.1

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile