Deserialize JSON to list with unknown object name in C#
Deserialize JSON to list with unknown object name in C#
I want to deserialize following JSON.
The problem is that the objects "ANDE" & "DAR" can change.
Means the objects are unknown and change depending on the JSON i wanna deserialize.
About 8000 different objects (ANDE, DAR, ...) need to be deserialized.
"ANDE":
"chart":[
"date":"20180914","minute":"09:30","date":"20180914","minute":"13:30"],
"DAR":
"chart":[
"date":"20180914","minute":"09:30","date":"20180914","minute":"13:30"]
I get the data by HTTP response and want to put into a List:
HttpResponseMessage response = client.GetAsync(API_PATH).GetAwaiter().GetResult();
List historicalDataList = response.Content.ReadAsAsync<List<HistoricalDataResponse>>().GetAwaiter().GetResult();
The HistoricalDataResponse class looks like:
public class HistoricalDataResponse
public string date get; set;
public string minute get; set;
How can i deserialize this kind of JSON with unknown objects in C#?
Any help is highly appreciated.
I updated my answer, please see
– Ashkan Mobayen Khiabani
Sep 15 '18 at 20:08
Did you try to deserialize to a dictionary ?
Dictionary<string, xxx>– Kalten
Sep 15 '18 at 21:00
Dictionary<string, xxx>
2 Answers
2
Then you should use a dynamic variable:
dynamic ReturnValue = JsonConvert.DeserializeObject(jsonstring);
note that as in dynamic objects, properties are determined after being assigned in runtime, so you will not get a drop down menu in design time, and also as its properties are unknown in design time, and property you test in design time even if its not correct, you wont get an error, and you will get the error in runtime when it is assigned.
dynamic ReturnValue = JsonConvert.DeserializeObject(jsonstring);
try
var a = ReturnValue.ANDE; // will work if it has ANDE property.
// do what you would do with ANDE
catch
try
var a = ReturnValue.DAR; // will work if it has DAR property.
// do what you would do with DAR
catch
I cant put ANDE in my running code as i wont know if "ANDE" is in the JSON. Maybe instead of "ANDE" there is "DCUD" or something else
– Daniel
Sep 15 '18 at 20:08
how many types are we talking about here?
– Ashkan Mobayen Khiabani
Sep 15 '18 at 20:09
There are about 8000 different types possible. So its not possible to put it into code. It needs to deserialize whatever object is in the JSON.
– Daniel
Sep 15 '18 at 20:14
Use a dictionary with string as key type :
void Main()
var client = new HttpClient();
HttpResponseMessage response = client.GetAsync("url").GetAwaiter().GetResult();
var json = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var result = JsonConvert.DeserializeObject<Dictionary<string, DateResponse>>(json);
foreach (var element in result)
var key = element.Key; // ANDE
foreach (var item in element.Value.Chart)
var date = item.date;
var minute = item.minute;
public class DateResponse
public List<HistoricalDataResponse> Chart get; set;
public class HistoricalDataResponse
public string date get; set;
public string minute get; set;
Thanks but i also need the ticker "ANDE" and "DAR" in the result. Otherwise i dont know which DataResponse is linked to which ticker. And how can i feed the DeserializeObject with the response.Content from the API?
HttpResponseMessage response = client.GetAsync(IEXTrading_API_PATH).GetAwaiter().GetResult();– Daniel
Sep 15 '18 at 21:37
HttpResponseMessage response = client.GetAsync(IEXTrading_API_PATH).GetAwaiter().GetResult();
DeserializeObject is like your ReadAsAsync. I used it for simplicity in the repro. If you need all data, just remove Select and SelectMany lines. The tickers will be the key in the dictionary– Kalten
Sep 15 '18 at 21:41
DeserializeObject
ReadAsAsync
The line with the DeserializeObject works but the code line
var result = response.Content.ReadAsAsync<Dictionary<string, DateResponse>>().GetAwaiter().GetResult(); does not feed my result.– Daniel
Sep 15 '18 at 21:58
var result = response.Content.ReadAsAsync<Dictionary<string, DateResponse>>().GetAwaiter().GetResult();
can you try with ReadAsStringAsync and then DeserializeObject ?
– Kalten
Sep 15 '18 at 21:59
I updated my answer
– Kalten
Sep 15 '18 at 22:26
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy
If you known each different pattern use a json.net TypeConverter newtonsoft.com/json/help/html/CustomJsonConverter.htm
– Kalten
Sep 15 '18 at 19:59