...and "beautiful" is sarcastic here.
When you call Active Campaign's list_view endpoint, and would like to get that in a json response, then this is the json response you get:
{
"0": {
"id": "4",
"name": "Nieuwsletter 1",
"cdate": "2018-11-22 03:44:19",
"private": "0",
"userid": "6",
"subscriber_count": 2901
},
"1": {
"id": "5",
"name": "Newsletter 2",
"cdate": "2018-11-22 05:02:41",
"private": "0",
"userid": "6",
"subscriber_count": 2229
},
"2": {
"id": "6",
"name": "Newsletter 3",
"cdate": "2018-11-22 05:02:48",
"private": "0",
"userid": "6",
"subscriber_count": 638
},
"result_code": 1,
"result_message": "Success: Something is returned",
"result_output": "json"
}
Now how would I ever be able to deserialize this to an object? Doing the normal Edit => Paste Special => Paste JSON As Classes gives me an output where I end up with classes that named _2.
Also, JsonConvert throws the following error: Accessed JObject values with invalid key value: 2. Object property name expected. So it is not really able to deserialize it either. I tried to use dynamic as object type to convert to.
The only thing I can think of now is replacing the first { by [ and the last } by ], then remove all the "1" : items and then remove the last 3 properties. After that I have a basic array which is easy convertable. But I kind of hope someone has a better solution instead of diving deep into the string.indexOf and string.Replace party...
If your key/value pair is not fixed and data must be configurable then Newtonsoft.json has one feature that to be used here and that is [JsonExtensionData]. Read more
Extension data is now written when an object is serialized. Reading and writing extension data makes it possible to automatically round-trip all JSON without adding every property to the .NET type you’re deserializing to. Only declare the properties you’re interested in and let extension data do the rest.
In your case key/value pair with 0,1,2,3.......N have dynamic data so your class will be
So create one property that collects all of your dynamic key/value pair with the attribute [JsonExtensionData]. And below I create that one with name DynamicData.
class MainObj
{
[JsonExtensionData]
public Dictionary<string, JToken> DynamicData { get; set; }
public int result_code { get; set; }
public string result_message { get; set; }
public string result_output { get; set; }
}
And then you can deserialize your JSON like
string json = "Your json here"
MainObj mainObj = JsonConvert.DeserializeObject<MainObj>(json);
Edit:
If you want to collect your dynamic key's value to class then you can use below the class structure.
class MainObj
{
[JsonExtensionData]
public Dictionary<string, JToken> DynamicData { get; set; }
[JsonIgnore]
public Dictionary<string, ChildObj> ParsedData
{
get
{
return DynamicData.ToDictionary(x => x.Key, y => y.Value.ToObject<ChildObj>());
}
}
public int result_code { get; set; }
public string result_message { get; set; }
public string result_output { get; set; }
}
public class ChildObj
{
public string id { get; set; }
public string name { get; set; }
public string cdate { get; set; }
public string _private { get; set; }
public string userid { get; set; }
public int subscriber_count { get; set; }
}
And then you can deserialize your JSON like
MainObj mainObj = JsonConvert.DeserializeObject<MainObj>(json);
And then you can access each of your deserialized data like
int result_code = mainObj.result_code;
string result_message = mainObj.result_message;
string result_output = mainObj.result_output;
foreach (var item in mainObj.ParsedData)
{
string key = item.Key;
ChildObj childObj = item.Value;
string id = childObj.id;
string name = childObj.name;
string cdate = childObj.cdate;
string _private = childObj._private;
string userid = childObj.userid;
int subscriber_count = childObj.subscriber_count;
}
I'd recommend JObject from the Newtonsoft.Json library
e.g. using C# interactive
// Assuming you've installed v10.0.1 of Newtonsoft.Json using a recent version of nuget
#r "c:\Users\MyAccount\.nuget\.nuget\packages\Newtonsoft.Json\10.0.1\lib\net45\Newtonsoft.Json.dll"
using Newtonsoft.Json.Linq;
var jobj = JObject.Parse(File.ReadAllText(#"c:\code\sample.json"));
foreach (var item in jobj)
{
if (int.TryParse(item.Key, out int value))
{
Console.WriteLine((string)item.Value["id"]);
// You could then convert the object to a strongly typed version
var listItem = item.Value.ToObject<YourObject>();
}
}
Which outputs:
4
5
6
See this page for more detail
https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm
Related
I've simplified the code (below) but I cannot figure out why the Result.Data property is not getting filled; it is always null. I've used jsonlint.com to validate the JSON (both this small sample and the full content). I built a separate project (using How to Deserialize a Complex JSON Object in C# .NET) and it successfully serializes the complex object listed there. But I cannot get this one to work and I'm stumped.
using System.Text.Json;
namespace JsonTest2;
public class Result
{
public string? Total { get; set; }
public string? Limit { get; set; }
public string? Start { get; set; }
protected List<Park>? Data { get; set; }
}
public class Park
{
public string? Id { get; set; }
}
internal class Program
{
var basepath = AppDomain.CurrentDomain.BaseDirectory;
var filepath = basepath.Split("\\bin")[0];
var filename = #$"{filepath}\NPS_response_small.json";
var jsonstr = File.ReadAllText(filename);
var response = JsonSerializer.Deserialize<Result>(jsonstr, new JsonSerializerOptions() { PropertyNameCaseInsensitive = true });
}
This is the content of "NPS_response_small.json":
{
"total": "468",
"limit": "50",
"start": "0",
"data": [
{
"id": "77E0D7F0-1942-494A-ACE2-9004D2BDC59E"
},
{
"id": "6DA17C86-088E-4B4D-B862-7C1BD5CF236B"
},
{
"id": "E4C7784E-66A0-4D44-87D0-3E072F5FEF43"
}
]
}
you have to chanbe a protected attribute of property Data to a public. Json deserializer doesnt have any acces to this property
public List<Park>? Data { get; set; }
it would be much easier to use Newtonsoft.Json, but if you need protected for some reason, you can try this ( but I am not sure that it is a full replacement)
public List<Park>? Data { protected get; init ; }
[System.Text.Json.Serialization.JsonConstructor]
public Result (List<Park>? Data, string? Total, string? Limit, string? Start)
{
this.Data=Data;
this.Total=Total;
this.Limit=Limit;
this.Start=Start;
}
I want to parse below JSON in ASP.NET.
{
"destination_addresses": [
"Address 1"
],
"origin_addresses": [
"Address 2"
],
"rows": [
{
"elements": [
{
"distance": {
"text": "15.7 km",
"value": 15664
},
"duration": {
"text": "17 mins",
"value": 1036
},
"status": "OK"
}
]
}
],
"status": "OK"
}
I want to retrieve the text and value from a distance token. I was able to reach till elements.
var objectjson = JObject.Parse(response.Content);
dynamic dynJson = JsonConvert.DeserializeObject(objectjson["rows"].ToString());
var elements = dynJson[0].ToString();
var firstElement = JObject.Parse(elements);
How to parse the json further to reach to the distance token and then text and value?
Create a class like:
public class Distance {
public string text { get; set; }
public int value { get; set; }
}
public class Duration {
public string text { get; set; }
public int value { get; set; }
}
public class Element {
public Distance distance { get; set; }
public Duration duration { get; set; }
public string status { get; set; }
}
public class Row {
public List<Element> elements { get; set; }
}
public class Root {
public List<string> destination_addresses { get; set; }
public List<string> origin_addresses { get; set; }
public List<Row> rows { get; set; }
public string status { get; set; }
}
then, you can convert on it and construct your logic easylly
var myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
Sites references:
Json2Csharp
NewtonSoft
If you don't want to design a data model corresponding to your JSON, you can access nested JToken values using its item indexer:
var distance = objectjson["rows"]?[0]?["elements"]?[0]?["distance"];
var text = (string)distance?["text"];
var value = (decimal?)distance?["value"]; // Or int or double, if you prefer.
You can also use SelectToken() to access nested values:
var distance = objectjson.SelectToken("rows[0].elements[0].distance");
Notes:
There is no need to reformat and reparse the JSON simply to access nested data. I.e. JsonConvert.DeserializeObject(objectjson["rows"].ToString()) is superfluous and will harm performance.
I am using the null-conditional operator ?[] to access nested tokens in case any of the intermediate properties are missing. If you are sure the properties are never missing or would prefer to throw an exception, you can do
var distance = objectjson["rows"][0]["elements"][0]["distance"];
Demo fiddle here.
Take this sample json and:
Go to visual studio
In the menu -> Edit -> Paste Special -> Paste JSON and Classes
VS will generate the classes for you. It will generate the root type as Root, rename it to whatever you want or leave it as Root. Then you can simply deserialise your string into that class.
var rootObject = JsonConvert.DeserializeObject<Root>(response.Content);
// Simply use rootObject to access any property
var firstElement = rootObject.rows[0].elements[0];
// firstElement.distance.text
// firstElement.distance.value
// firstElement.duration.text
// firstElement.duration.value
// firstElement.status
You can traverse the data this way:
' strBuf = your json string
Dim jOb As JObject = JObject.Parse(strBuf)
Dim MyDistance As JToken = jOb.SelectToken("rows[0].elements[0]")
After above, then
Text = MyDistance.SelectToken("distance.text").ToString
value = MyDistance.SelectToken("distance.value").ToString
Status = MyDistance.SelectToken("status").ToString
The above will get you the 3 values. Note that each of the rows are repeating data, so it not clear if you have one string, and with to pull the values, or pull multiple values? If you need to pull repeating data, then above would change.
The above is vb, but it quite much the same in c#.
I'm trying to retrieve all data from a JSON file to my C# application.
But now the problem is that the field "info" in my json file is sometimes from the type string but it can also be the type object.
{
[
{
"id":"147786",
"canUpdate":true,
"canDelete":true,
"canArchive":true,
"hasChildren":false,
"info": "Test"
},
{
"id":"147786",
"canUpdate":true,
"canDelete":true,
"canArchive":true,
"hasChildren":false,
"info": [{"id"="1","messages":"true"}]
}
]
}
well my model you can see here below, when there are only strings in my json file i can retrieve the data without any exception but when there are also objects in the info field then i get the error can't convert the value.
Is there a way to fix this on an easy way?
public string id { get; set; }
public string canUpdate { get; set; }
public string info { get; set; }
As an option you can define the info as dynamic:
public dynamic info { get; set; }
Example
Consider the following json string:
string json = #"
[
{ 'P1': 'X', 'P2': 'Y' },
{ 'P1': 'X', 'P2': [
{'P11':'XX', 'P22':'YY'},
{'P11':'XX', 'P22':'YY'}]
}
]";
You can define such model to deserialize it:
public class C
{
public string P1 { get; set; }
public dynamic P2 { get; set; }
}
And deserialize it like this:
var obj = JsonConvert.DeserializeObject<C[]>(json);
Note
If the number of dynamic properties is too much then usually there is no point in creating the class and the following code will be enough:
var obj = (dynamic)JsonConvert.DeserializeObject(json);
I'm receiving from an API a result that is something like:
[{
"propID": 1,
"propname": "nameA",
"dataType": "N",
"value": "9"
},
{
"propID": 2,
"propname": "nameB",
"dataType": "VL",
"value": "dasdsa"
},
{
"propID": 3,
"propname": "nameC",
"dataType": "N",
"value": "7"
},
{
"propID": 4,
"propname": "nameD",
"dataType": "VL",
"value": "jmfidsnjfs"
}
]
I'm getting this and decoding this into an DTO so I can convert the numeric values into numerics.
My DTO looks like:
public class PropertyToInsertDto
{
[JsonIgnore]
public int propID { get; set; }
public string propname { get; set; }
[JsonIgnore]
public string dataType { get; set; }
[JsonIgnore]
public string value { get; set; }
public string valueString { get; set; }
public float valueInt { get; set; }
}
So, imagining I store the API into string variable called result I would decode this using
var properties = JsonConvert.DeserializeObject<List<PropertyToInsertDto>>(result);
and then iterating each property to convert into numeric values
foreach(var property in properties) {
if (string.IsNullOrEmpty(property.value))
continue;
if (property.dataType == "N") {
property.valueInt = float.Parse(property.value);
} else {
property.valueString = property.value;
}
}
I want to convert this into Json so the result is
{"nameA": 9, "nameB":"dasdsa", "nameC":7, "nameD": "jmfidsnjfs"}
I tried using the SerializeObject method from JsonConvert without any good result.
My biggest problem is due to the fact that the result can come from valueInt or valueString depending if it is a number or a text.
Thanks!
Kuno
First of all you ignored "value" property, so this property isn't deserialized by JsonConvert and always has default value.
[JsonIgnore]
public string value { get; set; }
"valueString" and "valueInt" aren't required in this DTO, you need separated DTOs to read and write because you are changing object structure.
You can get expected result using this code:
var properties = JsonConvert.DeserializeObject<List<PropertyToInsertDto>>(str);
var result = JsonConvert.SerializeObject(properties.ToDictionary(
x => x.propname,
x => x.dataType == "N" ? (object)float.Parse(x.value) : x.value));
You can create a dictionary and then convert it to a json like this:
https://www.newtonsoft.com/json/help/html/SerializeDictionary.htm
Instead of int type as the value you can use object. Or use a string even for number types, but you would have to use custom convert type when deserializing in the future operations.
This might help as well:
Serializing/Deserializing Dictionary of objects with JSON.NET
I'm trying to parse the following json array
[
{
"email": "john.doe#sendgrid.com",
"timestamp": 1337197600,
"smtp-id": "<4FB4041F.6080505#sendgrid.com>",
"event": "processed"
},
{
"email": "john.doe#sendgrid.com",
"timestamp": 1337966815,
"smtp-id": "<4FBFC0DD.5040601#sendgrid.com>",
"category": "newuser",
"event": "clicked"
},
{
"email": "john.doe#sendgrid.com",
"timestamp": 1337969592,
"smtp-id": "<20120525181309.C1A9B40405B3#Example-Mac.local>",
"event": "processed"
}
]
I've not really used json format before, so it's all a little new. I found I can parse a single element easily, i.e.
{
"email": "john.doe#sendgrid.com",
"timestamp": 1337197600,
"smtp-id": "<4FB4041F.6080505#sendgrid.com>",
"event": "processed"
}
dynamic stuff = JsonConvert.DeserializeObject(json);
Response.Write(string.Format("{0} = {1}<br />", "timestamp", stuff.timestamp));
//etc
But i'm struggling with how to get the individual elements into an array to loop through.
I though about splitting the sting on },{ but didn't have much luck with that. I imagine there's an easier way i'm missing.
Thank you.
Just deserialize the JSON as is and loop it...
dynamic stuff = JsonConvert.DeserializeObject(json);
foreach (var s in stuff)
{
Console.WriteLine(s.timestamp);
}
Fiddle: http://dotnetfiddle.net/0SthDp
You can create a class like this one, to accept all properties from the json string:
public class MyClass
{
public string email { get; set; }
public long timestamp { get; set; }
[JsonProperty("smtp-id")]
public string smtpid { get; set; }
public string category { get; set; }
[JsonProperty("event")]
public string evt { get; set; }
}
As you can notice there is JsonProperty attribute on the smtpid and evt properties, because you can not use the names in the json string as properties in C#.
Then just call the following line:
var list = JsonConvert.DeserializeObject<List<MyClass>>(json);
and you'll get a strongly typed list of objects that matches the json string.
Use JSON.Net to do this for you:
Create a class to hold the data (notice the attribute I put on smtp-id to handle characters C# doesn't like):
public class EmailEvent
{
public string Email { get; set; }
public int TimeStamp { get; set; }
[Newtonsoft.Json.JsonProperty(PropertyName="smtp-id")]
public string SmtpId { get; set; }
public string Event { get; set; }
public string Category { get; set; }
}
Then just deserialize it:
var events = Newtonsoft.Json.JsonConvert.DeserializeObject<List<EmailEvent>>(System.IO.File.ReadAllText(#"z:\temp\test.json"));
foreach (var ev in events)
{
Console.WriteLine(ev.SmtpId);
}
Create these two classes to hold your data:
public class SMTPEvent {
public string Email { get; set; }
public long TimeStamp { get; set; }
public string SmtpId { get; set; }
public string EventType { get; set; }
}
public class SMTPEvents {
public List<SMTPEvent> Events { get; set; }
}
Then you can call the following:
dynamic stuff = JsonConvert.DeserializeObject<SMTPEvents>(json);
To iterate you can then use:
foreach(SMTPEvent sEvent in stuff)
{
//whatever you want to do.
}
The advantage to this approach is having more type safety at run-time whilst having reusable objects if you're going to use them in other parts of your system. If not, you might want to use the simpler dynamic approach as suggested by others.
Also, remember to use the JsonProperty attribute to specify the actual property name as specified in your JSON string if you cannot / are not going to create fields that have the exact name as in your JSON.
The first level - stuff - is an Array of objects. It is the objects, or elements in said array, which contain a timestamp field.
Consider the following:
dynamic items = JsonConvert.DeserializeObject(json);
foreach(dynamic item in items) {
/* use item.timestamp */
}