0

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

1 Answer 1

1

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
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

What you mean is that I need to have it formatted with the new lines inside the data token? I thought that deserializing or parsing wouldn't take into account the format as long as it was a proper json
The formatting indeed does not matter, but look carefully at what data holds. It's no longer a string (see difference between { ... } and "{ ... }").Your data property holds a string and not a structure with 3 properties. Right now it's a json in a json.
Ah! Didn't notice, thank you very much, I'll work on the formatting then

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.