AzeExMachina
AzeExMachina

Reputation: 104

Dynamic Json - Cannot access child value on Newtonsoft.Json.Linq.JValue

I'm trying to access a child value in my json, which looks like this

{
"event": "InstanceCreated",
"destination": "application",
"data": "{\"pipelineId\":1,\"requestId\":1,\"pid\":24740}" 
}

It's a string I get from an external process. I'm trying to access the requestId value doing

dynamic json = JsonConvert.DeserializeObject(s1);
var id = json.data.requestId;

But what I get is the exception mentioned in the title. I've read all of the similar issues but I couldn't find anything that solved mine. I thought about the issue where you could have too many escape chars like \ but it's not my case and doing Regex.Unescape won't do because it makes the string unparsable. I have also tried using JObject.Parse(s1) or any other class related parsing method but I always get that exception.

I can access some values of that json, doing things like

json.@event
json.destination
json.data

correctly returns me the associated value.

I'm using Newtonsoft.Json and Unity3D

Thanks for your help

Upvotes: 0

Views: 1288

Answers (1)

NotFound
NotFound

Reputation: 6272

Your data field is not an object with the 3 properties as you expect, but just a string in the way it's formatted now. You either need to get a proper format from the source or deserialize the string seperately to access the properties.

What it should look like to get the expected result:

{
    "event": "InstanceCreated",
    "destination": "application",
    "data": {
        "pipelineId": 1,
        "requestId": 1,
        "pid": 24740
    }
}

Upvotes: 1

Related Questions