Home > Net >  Need to get array of values from JSON data
Need to get array of values from JSON data

Time:07-22

I have the following JSON that is returned to me from an API call. I’m trying to just get the information from a property called “products”. Once I have that into an array or list I need to look for a specific product name and then one of it’s properties.

I’ve tried a handful of different things without any luck. I’m also searching and not finding what I’m looking for in my scenario.

If I use the following, I can get the data into parseJson object but from there I don’t understand how I can pull just the “products” into an array so I can loop through them looking for a specific product and it’s value.

dynamic parseJson = JsonConvert.DeserializeObject(response.Content);

I also tried this but had no luck either.

dynamic parseJson = JsonConvert.DeserializeObject<AccurateApiResult>(response.Content);

public class AccurateApiResult 
    {
        public List<AccurateProduct> products { get; set; }
    }

public class AccurateProduct
    {
        public int id { get; set; }
        public string productType { get; set; }
        public string status { get; set; }
        public string result { get; set; }
        public bool flag { get; set; }
    }

Here is the sample data and I’m only interested in the “products” section. How can I pull just that data?

[
    {
        "resource": "ORDER",
        "id": "Y10046727",
        "created": "2022-06-28T13:18:17Z",
        "updated": "2022-06-28T13:18:17Z",
        "workflow": "INTERACTIVE",
        "candidate": {
            "firstName": "Marcus",
            "lastName": "Willis",
            "middleName": null,
            "suffix": null,
            "dateOfBirth": "1990-01-01",
            "ssn": "111111111",
            "email": null,
            "phone": "240-5798551",
            "address": "3433 Lumar dr",
            "city": "Fort Washington",
            "region": "MD",
            "country": "US",
            "postalCode": "20744",
            "governmentId": {
                "country": "US",
                "type": null,
                "number": null
            },
            "aliases": [],
            "educations": [
                {
                    "school": "Test University",
                    "country": "US",
                    "region": "CA",
                    "city": "Irvine",
                    "degree": null,
                    "major": null,
                    "startDate": null,
                    "endDate": null,
                    "graduated": false,
                    "graduationDate": null,
                    "presentlyEnrolled": false
                }
            ],
            "prevEmployed": null,
            "employments": [],
            "convicted": null,
            "convictions": null,
            "references": [],
            "addressHistory": []
        },
        "completed": "2022-07-07T01:59:13Z",
        "supportReferenceId": "Y10046727",
        "status": "COMPLETE",
        "result": "Meets Requirements",
        "products": [
            {
                "id": 66134505,
                "productType": "AELS",
                "status": "COMPLETE",
                "result": "NOT APPLICABLE",
                "flag": false
            },
            {
                "id": 66134506,
                "productType": "ADJ",
                "status": "COMPLETE",
                "result": "NOT APPLICABLE",
                "flag": false
            },
            {
                "id": 66134508,
                "productType": "MOV",
                "status": "COMPLETE",
                "result": "Invalid SSN",
                "flag": false
            },
            {
                "id": 66144583,
                "productType": "MVR",
                "status": "COMPLETE",
                "result": "NOT APPLICABLE",
                "flag": false
            },
            {
                "id": 66144584,
                "productType": "F/M",
                "status": "COMPLETE",
                "result": "NO RECORD FOUND",
                "flag": false
            },
            {
                "id": 66144587,
                "productType": "EDU",
                "status": "COMPLETE",
                "result": "NOT APPLICABLE",
                "flag": false
            },
            {
                "id": 66144588,
                "productType": "DL5D",
                "status": "COMPLETE",
                "result": "Negative",
                "flag": false
            }
        ],
        "percentageComplete": 0,
        "candidateInfoChanged": false,
        "searchId": 66134503,
        "subjectId": 10121219,
        "requestor": "[email protected]"
    }

CodePudding user response:

you can try this code

var parsedJsonProducts = (JArray) JArray.Parse(response.Content)[0]["products"];

List<AccurateProduct> products = parsedJsonProducts.ToObject<List<AccurateProduct>>();
  • Related