Home > Enterprise >  How to extract the data for this type of Json Array with Newtonsoft Json
How to extract the data for this type of Json Array with Newtonsoft Json

Time:12-03

I am new to JSON in general. I have a JSON file and I want to extract data from it, but I can't seem to find a way on how to do it. I've searched online, but I could not find any answer or I was just looking at the wrong places.

Here is my JSON data:

{"data":
    {"cars":
        {"total":117,
        "results":[
            {"id":"779579"},
            {"id":"952209"},
            {"id":"1103285"},
            {"id":"1157321"},
            {"id":"1372321"},
            {"id":"1533192"},
            {"id":"1630240"},
            {"id":"2061824"},
            {"id":"2312383"},
            {"id":"2353755"},
            {"id":"2796716"},
            {"id":"2811260"},
            {"id":"2824839"},
            {"id":"2961828"},
            {"id":"3315226"},
            {"id":"3586555"},
            {"id":"3668182"},
            {"id":"3986886"},
            {"id":"3989623"},
            {"id":"3998581"},
            {"id":"4021057"},
            {"id":"4038880"},
            {"id":"4308809"},
            {"id":"4325718"},
            {"id":"4352725"},
            {"id":"4360349"},
            {"id":"4628661"},
            {"id":"4863093"},
            {"id":"4940146"},
            {"id":"4947395"},
            {"id":"5157781"},
            {"id":"5794466"},
            {"id":"6134469"},
            {"id":"6157337"},
            {"id":"6307352"},
            {"id":"6727975"},
            {"id":"6783794"},
            {"id":"6831800"},
            {"id":"6960771"},
            {"id":"7159286"},
            {"id":"7211880"},
            {"id":"7212277"},
            {"id":"7217410"},
            {"id":"7264660"},
            {"id":"7406984"},
            {"id":"7893798"},
            {"id":"7948268"},
            {"id":"8047751"},
            {"id":"8271106"},
            {"id":"8346001"},
            {"id":"8352176"},
            {"id":"8485193"},
            {"id":"8746468"},
            {"id":"8801718"},
            {"id":"9104008"},
            {"id":"9494179"},
            {"id":"9588599"},
            {"id":"9717878"},
            {"id":"9845048"},
            {"id":"9891941"},
            {"id":"9943516"},
            {"id":"10002374"},
            {"id":"10213949"},
            {"id":"10326370"},
            {"id":"10499431"},
            {"id":"10518069"},
            {"id":"10538037"},
            {"id":"10589618"},
            {"id":"10602337"},
            {"id":"10723171"},
            {"id":"10724725"},
            {"id":"10746729"},
            {"id":"10751575"},
            {"id":"10752559"},
            {"id":"10852235"},
            {"id":"10867573"},
            {"id":"10877115"},
            {"id":"10893349"},
            {"id":"10988880"},
            {"id":"10993485"},
            {"id":"11026957"},
            {"id":"11111205"},
            {"id":"11122085"},
            {"id":"11150052"},
            {"id":"11251748"},
            {"id":"11259887"},
            {"id":"11270391"},
            {"id":"11291731"},
            {"id":"11303142"},
            {"id":"11303143"},
            {"id":"11308615"},
            {"id":"11313379"},
            {"id":"11334337"},
            {"id":"11338119"},
            {"id":"11338290"},
            {"id":"11339650"},
            {"id":"11347202"},
            {"id":"11359983"},
            {"id":"11390048"},
            {"id":"11399541"}]}}}

I want to extract all the id and put them in an array. I tried JToken, but it can only get 100 data (up to element [99]) only because anything beyond 99 would give me an error. I tried it using a for loop.

This is the error I get if go beyond 99:

ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

CodePudding user response:

Your problem is that the value of data.cars.total is not what you think it is. In the JSON shown there are only 100 ids, despite the fact that data.cars.total equals 117. To put those 100 ids into an array, you may use SelectTokens() along with LINQ's ToArray() as follows:

var jtoken = JToken.Parse(jsonString); // Or load the JSON from a stream
var ids = jtoken.SelectTokens("data.cars.results[*].id").Select(id => (int)id).ToArray(); // Remove the .Select(id => (int)id) if you want them as strings

Console.WriteLine("{0} ids found:", ids.Length); // Prints 100
Console.WriteLine(string.Join(",", ids));        // Prints the deserialized ids

Where [*] is the JSONPath wildcard operator selecting all array elements.

Just a guess here, but possibly your JSON reflects a paged response in which data.cars.total is the total number of ids, not the number returned in the current page which was limited to 100.

Demo fiddle here.

CodePudding user response:

Thank you everyone for pointing out that the content of the array is only 100 and not 117. That solved the problem. The Json file was not returning the exact number of data that I expected (117). It turns out that my code was working fine all along and the problem is with the Json file.

  • Related