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

Dependency property never being set
How to keep the original document when projecting in mongodb?
Use split to remove parts of a string that are surrounded by curly double quotes
Installing json.net via NU get error 'Newtonsoft.Json' already has a dependency defined for Microsoft C#
Only insert data into a database if it doesn't exist already
Entity Framework not Finding Table
Windows Service Communicate WIth UI
How can I prevent screen recording using C#?
Angular $http.post() to C# Web API Controller
Return parameter in controller instead a View
How can I pause the audio source and animation at the same time in unity 3d
When should I use ServiceFabricIntegrationOptions.UseUniqueServiceUrl
Regex match and replace operators in math operation
ASP.NET login web application on intranet against Active Directory Domain
Why is the hub context connection ID inaccessible at runtime in my VERY simple method?
How to prevent the CSharpCodeProvider to output access modifiers

Categories

HOME
drupal
optimization
locale
grpc
apache-storm
c++builder
salesforce-lightning
perl6
sugarcrm
handlebars.js
subprocess
rename
theory
sha1
exchange-transport-agents
greensock
game-engine
vivado-hls
webgl-earth
vala
beaker-notebook
infinite-loop
connection-pooling
pumping-lemma
windows-mobile
visual-studio-community
gulp-jshint
angular2-changedetection
r-factor
mobile-development
opencsv
kubectl
epub
headphones
sampling
outlook-2010
mongoid5
form-fields
yelp
motion
usernotifications
julia-jump
ssdp
keychain
google-chrome-console
definition
jks
text-formatting
jenkins-docker
aot
multi-targeting
magick.net
hoare-logic
diameter-protocol
system-integration
audiorecord
transaction-isolation
laterjs
salesforce-service-cloud
dcast
firefox-os
yfiles
kramdown
event-tracking
nmock
maven-archetype
beagleboard
attiny
backtrace
drools-guvnor
random-access
system.io.packaging
nsurlrequest
go-gin
selenium-grid2
hotlinking
sendy
logo-lang
nsnumber
uikit-dynamics
qmainwindow
rbm
setcookie
gssapi
m3u
esi
report-viewer2010
android-memory
github-archive
django-admin-tools
diazo
win-prolog
facebook-winjs-sdk
rollover
chromebug
p4.net
for-xml
scalable
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