Reputation: 2708
I got the data from a identity check company in the form of JSON. I just need to get some data from that JSON file so I deserialized JSON file and tried to read a particular tag. I am always getting null value when I am trying to read the tag . Below is my JSON file and C# code:
{
"responseHeader": {
"requestType": "PreciseIdOnly",
"clientReferenceId": "21321",
"expRequestId": "213213",
"messageTime": "2020-05-28T00:00:02Z",
"overallResponse": {
"decision": "REFER",
"decisionText": "Continue & Investigate",
"decisionReasons": [
"Continue & Investigate"
],
"recommendedNextActions": [],
"spareObjects": []
},
"responseCode": "R123",
"responseType": "INFO",
"responseMessage": "Workflow Complete.",
"tenantID": "V123242"
},
"clientResponsePayload": {
"orchestrationDecisions": [
{
"sequenceId": "1",
"decisionSource": "PreciseId",
"decision": "REFER",
"decisionReasons": [
"Continue & Investigate"
],
"score": 737,
"decisionText": "Continue & Investigate",
"nextAction": "Continue",
"appReference": "22222",
"decisionTime": "2020-05-28T18:58:43Z"
}
]
}
}
Below is my code:
var data = (JObject)JsonConvert.DeserializeObject(JSONResponse);
string x = data["clientResponsePayload.orchestrationDecisions.sequenceId"].Value<string>();
JSONResponse is my JSON file from the company who is checking the identity. I get an error saying :
"Value cannot be null.\r\nParameter name: value
I am not sure what am I doing wrong. this JSON file is huge and have lot of nested JSON tags. This how I am Reading the JSON file send to me from the Identity check company:
HttpResponseMessage response = client.SendAsync(request).Result;
string responseContent = await response.Content.ReadAsStringAsync();
Upvotes: 2
Views: 1847
Reputation: 116981
You have a couple of problems.
Firstly, "clientResponsePayload.orchestrationDecisions.sequenceId"
is a JSONPath query. To query a JObject
via a JSONPath query you need to use JToken.SelectToken()
. The indexer you are using simply Gets or sets the JToken
with the specified property name and does not perform deep queries of the JSON hierarchy.
Secondly, the sequenceId
property is actually nested inside an array, so you need to use the path "clientResponsePayload.orchestrationDecisions[0].sequenceId"
to get the first one. Or, alternatively, you could use the path with the *
wildcard "clientResponsePayload.orchestrationDecisions[*].sequenceId"
along with JToken.SelectTokens()
to get all sequence ids.
Thus your code should be:
var x = data.SelectToken("clientResponsePayload.orchestrationDecisions[0].sequenceId")?.Value<string>();
Alternatively, rather than using a JSONPath query you could chain together indexers using the null-conditional operator ?[]
like so:
var x = data["clientResponsePayload"]?["orchestrationDecisions"]?[0]?["sequenceId"]?.Value<string>();
Demo fiddle here.
Upvotes: 1
Reputation: 1348
It's gonna be easier and more readable probably if you create a set of classes like this to get the value.
public class ReponseObject
{
public Payload ClientResponsePayload { get; set; }
}
public class Payload
{
public List<Decisions> OrchestrationDecisions { get; set; }
}
public class Decisions
{
public string SequenceId { get; set; }
}
And then do something like this:
ReponseObject jsonObject = JsonConvert.DeserializeObject<ReponseObject>(json);
string sequenceId = jsonObject.ClientResponsePayload.OrchestrationDecisions.First().SequenceId;
Anyway, if you don't want to create the classes you could do it like this:
data["clientResponsePayload"]["orchestrationDecisions"][0]["sequenceId"].Value<string>()
Upvotes: 1