Anjali
Anjali

Reputation: 2708

Deeply nested JSON Tag value

I have a very deeply nested JSON string - something like this:

{
    "decisionElements": [
        {
            "serviceName": "PreciseId",
            "applicantId": "APPLICANT_CONTACT_ID_1",
            "decision": "R12",
            "score": 123,
            "decisionText": "Refer",
            "appReference": "2323213",
            "rules": [],
            "otherData": {
                "json": {
                    "fraudSolutions": {
                        "response": {
                            "products": {
                                "preciseIDServer": {
                                    "sessionID": "2232GTH",
                                    "header": {},
                                    "messages": {},
                                    "summary": {},
                                    "preciseMatch": {},
                                    "kba": {
                                        "general": {
                                            "sessionID": "43124324",
                                            "numberOfQuestions": 4,
                                            "kbaresultCodeDescription": "processing successful questions returned",
                                            "kbaresultCode": 0
                                        },
                                        "questionSet": [
                                            {
                                                "questionType": 3,
                                                "questionText": "According to your credit profile, you may have opened an auto loan in or around June 1997. Please select the lender for this account. If you do not have such an auto loan, select 'NONE OF THE ABOVE/DOES NOT APPLY'.",
                                                "questionSelect": {
                                                    "questionChoice": [
                                                        "abc back",
                                                        "AUTO LEASE",
                                                        "FINANCE",
                                                        "BANK",
                                                        "NONE OF THE ABOVE/DOES NOT APPLY"
                                                    ]
                                                }
                                            }
                                        ]
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    ]
}

I am trying to retrieve the value of sessionID. I tried to do this based on the earlier stackoverflow post, but I am getting null for the sessionID. Below is how I am trying to retrieve the value:

var data = (JObject)JsonConvert.DeserializeObject(JSONResponse);
data.SelectToken("clientResponsePayload.decisionElements[0].otherData[0].json[0].fraudSolutions[0].response[0].products[0].preciseIDServer[0].sessionID")?.Value<string>()

Any help will be highly appreciated.

Upvotes: 1

Views: 66

Answers (2)

Abiola Akinnubi
Abiola Akinnubi

Reputation: 313

Here is an attempt by mapping the object to the json. json key must have same name as the class object for you to use this solution so I have attached the class I used using a feature in visual studio called paste json as class. ihave attached an image shot of the output enter image description here

var json = @"{'decisionElements': [
                {
                        'serviceName': 'PreciseId',
                    'applicantId': 'APPLICANT_CONTACT_ID_1',
                    'decision': 'R12',
                    'score': 123,
                    'decisionText': 'Refer',
                    'appReference': '2323213',
                    'rules': [],
                    'otherData': {
                        'json': {
                            'fraudSolutions': {
                                'response': {
                                    'products': {
                                        'preciseIDServer': {
                                            'sessionID': '2232GTH',
                                            'header': {},
                                            'messages': {},
                                            'summary': {},
                                            'preciseMatch': {},
                                            'kba': {
                                                'general': {
                                                    'sessionID': '43124324',
                                                    'numberOfQuestions': 4,
                                                    'kbaresultCodeDescription': 'processing successful questions returned',
                                                    'kbaresultCode': 0
                                                },
                                                'questionSet': [
                                                    {
                                                        'questionType': 3,
                                                        'questionText': 'According to your credit profile, you may have opened an auto loan in or around June 1997. Please select the lender for this account. If you do not have such an auto loan, select NONE OF THE ABOVE/DOES NOT APPLY.',
                                                        'questionSelect': {
                                                            'questionChoice': [
                                                                'abc back',
                                                                'AUTO LEASE',
                                                                'FINANCE',
                                                                'BANK',
                                                                'NONE OF THE ABOVE/DOES NOT APPLY'
                                                            ]
            }
        }
                                                ]
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            ]
        }";
                    var obj = JsonConvert.DeserializeObject<Rootobject>(json);
                    Console.WriteLine(obj.decisionElements.Select(h=>h.otherData.json.fraudSolutions.response.products.preciseIDServer.sessionID).FirstOrDefault());

      public class Rootobject
        {
            public Decisionelement[] decisionElements { get; set; }
        }

        public class Decisionelement
        {
            public string serviceName { get; set; }
            public string applicantId { get; set; }
            public string decision { get; set; }
            public int score { get; set; }
            public string decisionText { get; set; }
            public string appReference { get; set; }
            public object[] rules { get; set; }
            public Otherdata otherData { get; set; }
        }

        public class Otherdata
        {
            public Json json { get; set; }
        }

        public class Json
        {
            public Fraudsolutions fraudSolutions { get; set; }
        }

        public class Fraudsolutions
        {
            public Response response { get; set; }
        }

        public class Response
        {
            public Products products { get; set; }
        }

        public class Products
        {
            public Preciseidserver preciseIDServer { get; set; }
        }

        public class Preciseidserver
        {
            public string sessionID { get; set; }
            public Header header { get; set; }
            public Messages messages { get; set; }
            public Summary summary { get; set; }
            public Precisematch preciseMatch { get; set; }
            public Kba kba { get; set; }
        }

        public class Header
        {
        }

        public class Messages
        {
        }

        public class Summary
        {
        }

        public class Precisematch
        {
        }

        public class Kba
        {
            public General general { get; set; }
            public Questionset[] questionSet { get; set; }
        }

        public class General
        {
            public string sessionID { get; set; }
            public int numberOfQuestions { get; set; }
            public string kbaresultCodeDescription { get; set; }
            public int kbaresultCode { get; set; }
        }

        public class Questionset
        {
            public int questionType { get; set; }
            public string questionText { get; set; }
            public Questionselect questionSelect { get; set; }
        }

        public class Questionselect
        {
            public string[] questionChoice { get; set; }
        }

Upvotes: 1

Mohammed Sajid
Mohammed Sajid

Reputation: 4903

For the Json posted in the question :
1 - You don't have clientResponsePayload in the Json
2 - For the SessionId path, you have one array decisionElements and the other tags are just objects

The following code populate the SessionId:

var data = (JObject)JsonConvert.DeserializeObject(JSONResponse);
string seesionId = data.SelectToken("decisionElements[0].otherData.json.fraudSolutions.response.products.preciseIDServer.sessionID")?.Value<string>();

The path :

decisionElements[0].otherData.json.fraudSolutions.response.products.preciseIDServer.sessionID

I hope you find this helpful.

Upvotes: 1

Related Questions