0

I have some JSON like this :

[{
    "Headers": ["Building ID",
    "Building",
    "Active",
    "Last Updated Date"],
    "ID": "Table_1890",
    "Rows": [["1",
    "2 Chifley Tower, Sydney",
    "True",
    ""],
    ["5",
    "60 Martin Place, Sydney",
    "True",
    ""],
    ["11",
    "275 Kent Street, Sydney",
    "True",
    ""],
    ["16",
    "360 Collins Street, Melbourne",
    "False",
    ""]]
},
{
    "Headers": ["FloorID",
    "Floor",
    "Active",
    "Last Updated Date"],
    "ID": "Table_1890",
    "Rows": [["1",
    "2 Chifley Tower, Sydney",
    "True",
    ""],
    ["5",
    "60 Martin Place, Sydney",
    "True",
    ""],
    ["11",
    "275 Kent Street, Sydney",
    "True",
    ""],
    ["16",
    "360 Collins Street, Melbourne",
    "False",
    ""]]
}]

I want to query specific JSON array using Headers property. for example I want to get the array where "Headers": ["Building ID","Building","Active","Last Updated Date"]. So this should return me the first array which includes the headers, ID, rows. I really appreciate any help as to how can I achieve this. I am using Newtonsoft and I am deserializing this array.

Currently I do this. This gives errors:

string json = File.ReadAllText(fileSavePath+"\\1240.txt");
JArray ja = JArray.Parse(json); 
JObject match = ja.Values<JObject>()
    .Where(m => m["Headers"].Value<string>() == "[\"Building ID\",\"Building\",\"Active\",\"Last Updated Date\"]")
    .FirstOrDefault();
3
  • There are a few unclear things in your question. Do you want the object where the Headers array contains exactly these items? Do you include Json.Net? Do you want to deserialize that Json? Do you have C# classes for that data structure? Commented Mar 2, 2016 at 6:26
  • Eddited the question. And i want the headers and rows containing array. lets say first one Commented Mar 2, 2016 at 6:30
  • Which data structure are you serializing into? Commented Mar 2, 2016 at 6:45

1 Answer 1

1

Note: compare string like your solution is not safe, you might need to remove white space and trim it. I think you need to find more elegant way to compare the header value.

Below is working code:

JArray objects = JsonConvert.DeserializeObject<JArray>(json);

var compare = @"[
""BuildingID"",
""Building"",
""Active"",
""LastUpdatedDate""
]";

JObject match = objects.Children<JObject>().FirstOrDefault(o => o["Headers"] != null && o["Headers"].ToString().Replace(" ", "") == compare);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You. This is what i was looking for

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.