0

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).

1
  • Can you getting rid of the Tags class and instead add this as a property for the Tags? public IDictionary<string, JToken> Tags { get; set; } Commented Dec 11, 2018 at 5:01

2 Answers 2

2

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"

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

4 Comments

How is this different from a simple JsonConvert.DeserializeObject<IDictionary<string, object>>(json)? can't see how this helps me.
Consider a more complex data type than shown in example, perhaps something like yours. In that scenario too this would work.
Can you show me how you would do this with my json schema rather than this simplistic example you've shown?
@swdon I have updated the answer with sample code with your data structure
0

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; }
}

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.