I have a JSON string that I'm getting from Facebook API, in which I have a node whose name changes according to its content, for example some time it is 45, or 58 etc.
It could be any number.
I want its value. How to get it?
Example:
{
"data": [
{
"id": "1492292372_10201810786059989",
"created_time": "2014-04-05T09:00:54+0000"
},
{
"id": "1492292372_10201804679827337",
"created_time": "2014-04-04T07:29:07+0000"
},
{
"id": "1492292372_10201804649306574",
"created_time": "2014-04-04T07:10:33+0000"
},
{
"id": "1492292372_10201801316823264",
"created_time": "2014-04-03T18:31:50+0000"
},
{
"id": "1492292372_10201798962284402",
"created_time": "2014-04-03T06:24:47+0000"
},
{
"message_tags": {
"0": [
{
"id": "1492292372",
"name": "Yawar Sohail",
"type": "user",
"offset": 0,
"length": 12
}
],
"15": [
{
"id": "1489845168",
"name": "Zeeshan Anjum",
"type": "user",
"offset": 15,
"length": 13
}
]
},
"id": "1492292372_10201796274777216",
"created_time": "2014-04-02T17:57:05+0000"
},
{
"id": "1492292372_10201794080482360",
"created_time": "2014-04-02T07:26:23+0000"
},
Inside message_tags there are two nodes [0 and 15] they dynamically changes according to their offset values. I want names, type and ids inside these nodes.
You can deserialize your JSON into an ExpandoObject:
var converter = new ExpandoObjectConverter();
dynamic obj = JsonConvert.DeserializeObject<ExpandoObject>(json, converter);
Which dynamically adds members to your object at runtime, and allows you to iterate over them as described in this answer:
foreach (var prop in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public))
{
Console.WriteLine("Name: {0}, Value: {1}",prop.Name, prop.GetValue(obj,null));
}
That way you can iterate over obj.message_tags to get the individual messages, and obtain all their details respectively.
Related
could anyone help me extract the specific value for [downloads][csv][href] out of the following json string please?
{
"#context": "https://cdn.ons.gov.uk/assets/json-ld/context.json",
"alerts": [],
"collection_id": "cmdr",
"dimensions": [
various dimensions here
],
"downloads": {
"csv": {
"href": "https://download.beta.ons.gov.uk/downloads/datasets/regional-gdp-by-year/editions/time-series/versions/4.csv",
"size": "568021"
},
"csvw": {
"href": "ref",
"size": "1903"
},
"xls": {
"href": "ref",
"size": "80768"
}
},
"edition": "time-series",
"id": "5c",
"links": {
"dataset": {
"href": "ref",
"id": "regional-gdp-by-year"
},
"edition": {
"href": "ref",
"id": "time-series"
},
"self": {
"href": "ref"
}
},
"release_date": "2021",
"state": "published",
"usage_notes": [],
"version": 4
}
the current code I am using returns nothing:
var labels = JObject.Parse(observationsJson)["downloads"];
var results = labels.Select(data => new
{
label = (string)data["href"]
});
foreach (var item in results)
{
csvDownloadAddress = item.label;
}
If there is another method rather than using linq then I am open to suggestions.
Sorry if this is a dumb quest, my experience with linq and json is very limited.
thank you for your help.
You have JSON Path, that can be used to select specific element(s) from the JSON string.
see Newtonsoft example, or you can use this JsonPath Nuget package.
The JsonPath Nuget can be used like below,
var input = "$.downloads.csv.href";
var path = JsonPath.Parse(input);
var hrefVal = path.Evaluate(jsonStr);
How can I extract items from nested Json Array using Newtonsoft.Json functions or methods? I have a Json like this
{
"Bounds": {
"TextLength": 1379
},
"DocumentTypeName": "Invoice",
"DocumentTypeField": {
"Value": "Invoice",
"Confidence": 1
},
"Fields": [
{
"FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems",
"FieldName": "Line Items",
"Values": [
{
"Components": [
{
"FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems.Body",
"FieldName": "Body",
"Values": [
{
"Components": [
{
"FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems.Item",
"FieldName": "Item",
"Values": [
{
"Components": [],
"Value": "Film 4C for the publication \"Racing World\" Visual: PNSP 02 05 Ref. 2004/021 Graphic designer honoraries 560010",
"Confidence": 0.962736368
}
]
},
{
"FieldId": "RPA.DocumentUnderstanding.Invoice.LineItems.UnitPrice",
"FieldName": "Unit Price",
"Values": [
{
"Components": [],
"Value": "400.00",
"Confidence": 0.9779528
}
]
}
],
"Confidence": 0.9432406
}]}],
"Confidence": 0.920952857}]}]}
and I want to extract the red highlighted fields from it.
Any help will be much appreciated.
Thanks
Since not deserializing is not a requirement you can do that. Just create C# object that has the exact same structure as your JSON and then do
var yourObject = JsonConvert.DeserializeObject<YourCSharpClassHere>(yourJsonString);
Then it's just a simple matter of getting the values
var fieldName = yourObject.Values[0].Components[0].Values[0].Components[0].FieldName
You can use JSON Query
Example
var fieldNames = o.SelectTokens("Values[*].Components[*].Values[*].Components[*].FieldName");
I've this JSON object
{
"08f4f705-6e14-4781-8241-d04bf2dc6ada": {
"description": "xxxxxxxx",
"note": "yyyyyyyy"
},
"05f4f995-6e14-4567-8241-d04bf2d456ee": {
"description": "aaaaaa",
"note": "bbb"
},
"0675f995-6e14-4567-8241-d4567f2d456z": {
"description": "fffff",
"note": "gggg"
}
}
I need to convert into a JSON array like this:
(the elements should be the content of the first level properties)
[
{
"description": "xxxxxxxx",
"note": "yyyyyyyy"
},
{
"description": "aaaaaa",
"note": "bbb"
},
{
"description": "fffff",
"note": "gggg"
}
]
I can't manipulate the object and I didn't find an appropriate resource to follow. How can I do it?
You can achieve this by deserializing your json string into Dictionary<string, object>:
var obj = JsonConvert.DeserializeObject<Dictionary<string, object>>(json);
After that you extract the values and serialize them back to json:
var newJson = JsonConvert.SerializeObject(obj.Values);
I want to extract all IDs from the below object using c#.
in need of extracting a response value in a visual studio webtest.
The JSON is obtained using extract event.
{
"d": [
{
"__type": "QuestionZoneEditor.Entities.QuestionTag",
"Id": 2080,
"Name": "01",
"Items": [
"1a",
"1b",
"1c",
"1d"
]
},
{
"__type": "QuestionZoneEditor.Entities.QuestionTag",
"Id": 2081,
"Name": "02",
"Items": [
"2a(i)",
"2a(ii)",
"2b",
"2c"
]
},
{
"__type": "QuestionZoneEditor.Entities.QuestionTag",
"Id": 2082,
"Name": "03",
"Items": [
"3a",
"3b",
"3c"
]
}
}
]
}
Deserialize it to JObject after that take all JObject from the JArray and print the Id
var result = JsonConvert.DeserializeObject<JObject>(json);
foreach(JObject obj in result["d"])
{
Console.WriteLine(obj["Id"]);
}
Full example: dotNetFiddle
I am working on Visual Studio C# project and I need to convert a JSON to XML.
I receive the JSON in string format.
The problem is, I need to have a root node in the JSON structure if the JSON doesn't have one, so that I can convert to XML with desired format.
Supose I have this JSON:
{
"id": 1,
"name": {
"first": "Yong",
"last": "Mook Kim"
},
"contact": [{
"type": "phone/home",
"ref": "111-111-1234"
}, {
"type": "phone/work",
"ref": "222-222-2222"
}]
}
And I want to add root node to that JSON just like that:
{
"user": {
"id": 1,
"name": {
"first": "Yong",
"last": "Mook Kim"
},
"contact": [{
"type": "phone/home",
"ref": "111-111-1234"
}, {
"type": "phone/work",
"ref": "222-222-2222"
}]
}
}
How can I do it with C# and JSON.NET?
I suppose you have user object. Just use anonymous class to add extra root node:
var obj = new { user = user };
string json = JsonConvert.SerializeObject(obj);
The resulting JSON will look like that:
{
"user": {.../your user object/...}
}