37

I have this JSON:

{
    "client_id": "26075235",
    "client_version": "1.0.0",
    "event": "app.uninstall",
    "timestamp": 1478741247,
    "data": {
        "user_id": "62581379",
        "site_id": "837771289247593785",
        "platform_app_id": "26075235"
    }
}

I parse it into a JSON.NET JObject and I can successfully access the first level of values using e.g. (string)RequestBody.SelectToken("client_id")

How do I access the value of "user_id" using a JPath expression (or by accessing a child object of the JSON.NET JObject)? This doesn't work:

(string)RequestBody.SelectToken("data[0].user_id")

and I can't do this to parse the 'data' part of the JSON:

JObject RequestBodyData = JObject.Parse((string)RequestBody.SelectToken("data"));

as the compiler seems to recognise RequestBody.SelectToken("data") as an object (I get the error 'Can not parse object into string')

and I don't want to parse the original JSON into a custom C# object as I'm developing a solution that needs to be able to generically parse JSON into a JObject (or any other type of generic object for handling JSON), so it can be parsed in a relatively consistent way.

1 Answer 1

47

SelectToken("data[0].user_id") doesn't work because there isn't an array in your JSON. You should use SelectToken("data.user_id") instead.

Fiddle: https://dotnetfiddle.net/K0X4ht

Sign up to request clarification or add additional context in comments.

Comments

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.