-1

Already checked this post but that did not solve my situation : Parsing a JSON array using Json.Net

I have a API response which returns me a JSON array, like the following :

{
    "status": {
        "code": "200",
        "description": "OK"
    },
    "response": [{
        "weId": "1",
        "weName": "env1"
    },{
        "weId": "2",
        "weName": "env2"
    },{
        "weId": "3",
        "weName": "env3"
    }]
}

Here's my question :

This array might return more than 2 values.

What i mean is like this :

{
    "response": [{
        "weId": "1",
        "weName": "env1",
        "otherAttribute1": "otherValue1",
        "otherAttribute2": "otherValue2",
        "otherAttribute3": "otherValue3"
    }]
}

How am I able to dynamically parse a JSON array, which has an unknown dimension ?

Thanks in advance.

4

1 Answer 1

3

Json.Net can dynamically parse into a JObject.

var unParsed = @"{
    ""response"": [{
        ""weId"": ""1"",
        ""weName"": ""env1"",
        ""otherAttribute1"": ""otherValue1"",
        ""otherAttribute2"": ""otherValue2"",
        ""otherAttribute3"": ""otherValue3""
    }]
}";

dynamic d = JObject.Parse(unParsed);
Console.WriteLine(d.response[0].weId);
Console.WriteLine(d.response[0].otherAttribute1);

foreach (var r in d.response)
{
    foreach (var prop in r.Properties())
    {
        Console.WriteLine("{0} - {1}", prop.Name, prop.Value);
    }
}

Now, that being said, you may not need to do it dynamically. If you have a known set of optional parameters, you can deserialize it into a strongly typed object and anything missing will just be the default value (0 for ints, null for classes, etc), as in the answer below:

public static void JsonParsing()
{
    var unParsed = @"{
        ""status"": {
            ""code"": ""200"",
            ""description"": ""OK""
        },
        ""response"": [{
            ""weId"": ""1"",
            ""weName"": ""env1""
        },{
            ""weId"": ""2"",
            ""weName"": ""env2"",
            ""otherAttribute1"": ""value1"",
            ""otherAttribute2"": ""value2"",
            ""otherAttribute3"": ""value3""
        },{
            ""weId"": ""3"",
            ""weName"": ""env3""
        }]
    }";

    var parsed = JsonConvert.DeserializeObject<Rootobject>(unParsed);
    Console.WriteLine(parsed.Response[0].OtherAttribute1); // writes a "" since it's null
    Console.WriteLine(parsed.Response[1].OtherAttribute1); // writes "Value1"
    Console.WriteLine(parsed);
}
7
  • It's usually best to avoid dynamic unless you have a specific reason to use it.
    – mason
    Commented Mar 3, 2015 at 20:35
  • 1
    He asked how to dynamically parse a JSON. I'm going to trust he has a reason to want to do it dynamically. I don't see why I should be downvoted for answering his question.
    – Pharylon
    Commented Mar 3, 2015 at 20:49
  • Because using dynamic doesn't appear to be necessary to parse this JSON. You can parse to a strongly typed object. Like I said, dynamic should be avoided unless you have a specific reason to use it. Anyways, assuming that dynamic is necessary, this would be a duplicate question.
    – mason
    Commented Mar 3, 2015 at 20:54
  • 1
    @mason If the properties that exist on the object are arbitrary, it is impossible to make a type that can hold the data at compile time, since the shape of the data is unknown at compile time. This is a perfectly valid use of dynamic. Commented Mar 4, 2015 at 20:43
  • 1
    @Pharylon That's better, I've removed my downvote. I still don't think using dynamic is a good approach, at least for deserializing that the top level. Perhaps deserializing to a model object that uses a dictionary or dynamic property would be better. But your code is now more robust so I'm satisfied.
    – mason
    Commented Mar 4, 2015 at 21:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.