I am looking a way to get the specific value from complex json
without creating the Model class. I am able to read via Newtonsoft
but with model and since I have many different json
model,is it possible to get the value of content.result.id
directly ?
Is ther any way to get it as my json keeps on changing:
Json:
{
"id": "123",
"Type": "Test",
"content": {
"id": "abcd",
"testResult": "Failed",
"field1": {
"id": "T1",
"name": "T1 name"
},
"filed2": {
"id": "T2",
"name": "T2 name "
},
"result": {
"id": "1111",
"name": "T3 name"
}
},
"createdAt": "2018-10-11T13:18:54.0555456+01:00",
"updatedAt": "2018-10-11T13:18:54.0555518+01:00",
"deleted": false,
"_rid": "AAAA===",
"_self": "Ab++/",
"_etag": ""1qw"",
"_attachments": "attachments/",
"_ts": 12345
}
Expected outcome: 1111 (from within result)
4
Answers
With Newtonsoft.Json, read the JSON as
JObject
and get the value with JSON Path.Reference: Querying JSON with complex JSON Path
if you are using
System.Text.Json
you can useJsonNode
as Follows :or if you are using
Newtonsoft
you can useJObject
as Follows:You need to add null checking, but that will work.
You can fetch the specific JSON field value without defining a model class by using Newtonsoft’s JObject class
…
…