I have a problem with this JSON to get the second level in JArray:
{
"Level1": {
"Level2": [{
"id": "Chart",
"Box": [{
"id": "1",
"value": "10"
},{
"id": "2",
"value": "20"
}]
}]
}
}
And I want to get completly the level 2 like Array in this way:
JArray contasdasd = _jsonMaster["Level1"]["Level2"] as JArray;
I get :
{
"id": "Chart",
"Box": [{
"id": "1",
"value": "10"
},{
"id": "2",
"value": "20"
}]
}
and I want:
"Level2": [{
"id": "Chart",
"Box": [{
"id": "1",
"value": "10"
},{
"id": "2",
"value": "20"
}]
}]
Is there any way to get the level 2 ?
Surely you just do
var level1 = _jsonMaster["Level1"];
The problem is that you've gone one level too far.
The object you are interested in "Level2": [...] is not a JArray itself but is a JProperty. You can get it by searching properties of Level1 but keep in mind you will now have a JProperty rather than a JArray. The Value of that result will be the JArray you were getting in your current code.
JProperty contasdasd = _jsonMaster["Level1"].First(o => (o as JProperty).Name == "Level2") as JProperty;
Console.WriteLine(contasdasd.ToString()); // Will be what you are looking for
Console.WriteLine(contasdasd.GetType()); // Will return JProperty
Console.WriteLine(contasdasd.Value.GetType()); // Will return JArray
Related
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");
this my structure
{
"_id": ObjectId,//PropertyTrackingModel Object
"Items": [{
"Date": "2018-01-29",
"Tracks": [{
"ModifiedDate": "2018-06-29T10:21:03.1268154+00:00"
}]
}, {
"Date": "2018-08-29",
"Tracks": [{
"ModifiedDate": "2018-08-29T10:21:03.1268154+00:00"
}]
}]
}
so i want insert New Tracks in date=="2018-08-29" collection
and it like daily data
finaly i need
{
"_id": ObjectId,//PropertyTrackingModel Object
"Items": [{
"Date": "2018-01-29",
"Tracks": [{
"ModifiedDate": "2018-06-29T10:21:03.1268154+00:00"
}]
}, {
"Date": "2018-08-29",
"Tracks": [{
"ModifiedDate": "2018-08-29T10:21:03.1268154+00:00"
},
{
"ModifiedDate": "2018-08-29T10:21:03.1268154+00:00"
}]
}]
}
thank all of you
bast way using C# mongo driver function
I have all values from aspx page. now I want to prepare following json values by picking up from the control values of aspx where user has inputted and want to submit to other application in same format.For that below is the example I want to make exact copy as like below.
var tempk = {
"requestTypeCode": "PRE_DETERMINATION",
"billingProvider": {
"npi": "1234567893",
"ein": "111222333",
"payerAssignedProviderId": "XYZ321"
},
"patient": {
"relationshipCode": "01",
"lastName": "Smith",
"firstName": "Bob",
"stateCode": "FL",
"birthDate": "1980-02-12",
"genderCode": "M"
},
"payer": {
"id": "BCBSF"
},
"submitter": {
"id": "123456789",
"lastName": "SUBMITTER"
},
"subscriber": {
"memberId": "JDH001",
"groupName": "ASDF 1-2",
"groupNumber": "12312412"
},
"claimInformation": {
"placeOfServiceCode": "11",
"diagnoses": [
{
"qualifierCode": "ABK",
"code": "J3089"
}
],
"serviceLines": [
{
"procedureCode": "92523",
"quantity": "100",
"amount": "250",
"fromDate": "2016-05-10"
}
]
}
}
Can you assist me how with the C# code will achieve?
You can use Newtonsoft.Json from nuget
http://www.newtonsoft.com/json
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 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.