kovac
kovac

Reputation: 5389

How to deserialize complex JSON with dynamic keys in asp.net core?

I have this complex JSON that looks like:

{
    "results": [
        {
            "statement_id": 0,
            "series": [
                {
                    "name": "test-name",
                    "tags": {
                        "tag1": "0"
                        "tag2": "1"
                    },
                    "columns": [
                        "time",
                        "mean"
                    ],
                    "values": [
                        [
                            "2018-12-03T10:18:37.3Z",
                            0
                        ]
                    ]
                }
            ]
        }
    ]
}

The POCO for the above is like:

public class Tags
{
    public string tag1 { get; set; }
    public string tag2 { get; set; }
}

public class Series
{
    public string name { get; set; }
    public Tags tags { get; set; }
    public List<string> columns { get; set; }
    public List<List<object>> values { get; set; }
}

public class Result
{
    public int statement_id { get; set; }
    public List<Series> series { get; set; }
}

public class RootObject
{
    public List<Result> results { get; set; }
}

The problem is, properties in Tags class are dynamic. There can be just one tag, 2 or more which is only determined at runtime.

I'm trying to use Newtonsoft.Json to parse this to a nested dictionary like Dictionary<string, <Dictionary<string, object[]>>> where the keys of the dictionaries are values of tags and the object[] contains the list of values (just the value, not the timestamp).

Upvotes: 0

Views: 1524

Answers (2)

Anu Viswan
Anu Viswan

Reputation: 18153

You should possibly use Dictionary instead of a Tag Class.

[JsonExtensionData]
public Dictionary<string,object> Tags{get;set;}

For example,

public class JsonSample
{
[JsonExtensionData]
public Dictionary<string,object> RandomKeyValuePair {get;set;}
}

// Sample Code
var jsonString = @"
{
'Key1' : 'some value 1',
'Key2' : 'some value 1',
'Key3' : 'some value 1',
'Key4' : 'some value 1',
}";



var jsonSampleObject = JsonConvert.DeserializeObject<JsonSample>(jsonString);

Update

Modified Series Class

public class Series
{
    public string name { get; set; }
    [JsonExtensionData]
    public IDictionary<string,JToken> tags { get; set; }
    public List<string> columns { get; set; }
    public List<List<object>> values { get; set; }
}

Client Call

var result = JsonConvert.DeserializeObject<RootObject>(str);
Console.WriteLine(String.Join(",",result.results.First().series.First().tags.Select(x=>$"{x.Key}={x.Value},")));

Output

tags={
  "tag1": "0",
  "tag2": "1"
},

Please note there is an error in your Json. "," is missing after "tag1": "0"

Upvotes: 2

er-sho
er-sho

Reputation: 9771

Only change your tags property datatype in Series class from

public Tags tags { get; set; }

To

public Dictionary<string, JToken> tags { get; set; }

And then you will be able to parse dynamic key tags from your json.

So your Series class will be.

public class Series
{
    public string name { get; set; }
    public Dictionary<string, JToken> tags { get; set; }
    public List<string> columns { get; set; }
    public List<List<object>> values { get; set; }
}

Upvotes: 0

Related Questions