From an external API I am receiving the below JSON response for the bank details of a customer.
{
"bankDetails":[
{
"ABC Bank":[
{
"sNo":1,
"acNo":"1235465",
"acBalance":"100.25"
},
{
"sNo":2,
"acNo":"1235467",
"acBalance":"50.25"
}
],
"bankName":"ABC Bank",
"totalAmount":"150.50"
},
{
"XYZ Bank":[
{
"sNo":1,
"acNo":"1248565",
"acBalance":"75.25"
}
],
"bankName":"XYZ Bank",
"totalAmount":"75.25"
},
{
"BCD Bank":[
{
"sNo":1,
"acNo":"145665",
"acBalance":"10.25"
},
{
"sNo":2,
"acNo":"195267",
"acBalance":"5.25"
}
],
"bankName":"BCD Bank",
"totalAmount":"15.50"
}
]
}
I need to deserialize this to a C# class using JSON.Net. What should be structure of the C# class as the first key is dynamic?. The first key with bank name returned will be different for each customer
The typical solution to dealing with dynamic keys is to use a Dictionary<string, T> in place of a regular class. See How can I deserialize a child object with dynamic (numeric) key names? for an example of this. However, that solution doesn't really work for your case, because there are other properties in the same object which do not have dynamic keys (the bankName and totalAmount), and the values of those properties are primitives whereas the value of dynamic property is an array of bank accounts. A better solution here is to use a JsonConverter.
Before we get to that, we need to set up a class structure to deserialize into. This is pretty straightforward:
class RootObject
{
public List<Bank> BankDetails { get; set; }
}
[JsonConverter(typeof(BankConverter))]
class Bank
{
public string BankName { get; set; }
public decimal TotalAmount { get; set; }
public List<Account> Accounts { get; set; }
}
class Account
{
[JsonProperty("sNo")]
public int SequenceNumber { get; set; }
[JsonProperty("acNo")]
public string AccountNumber { get; set; }
[JsonProperty("acBalance")]
public decimal Balance { get; set; }
}
You'll notice that I've added a few [JsonProperty] attributes in the Account class to map the shorthand property names in the JSON to friendlier property names in that class. And the [JsonConverter] attribute on the Bank class tells the serializer that we will be using a custom BankConverter to handle that class.
Here is the code for the BankConverter. It uses a JObject internally to make it easier to read and work with the JSON.
class BankConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(Bank);
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
JObject obj = JObject.Load(reader);
Bank bank = new Bank();
// populate the known properties (bankName and totalAmount)
serializer.Populate(obj.CreateReader(), bank);
// now handle the dynamic key
bank.Accounts = obj[bank.BankName].ToObject<List<Account>>(serializer);
return bank;
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
With these classes in place, you can deserialize the JSON like this:
var root = JsonConvert.DeserializeObject<RootObject>(json);
Here is a working demo: https://dotnetfiddle.net/knsRLv
Related
I'm using this git repository JsonApiSerializer with a project and tying to update to the latest version 1.74. Everything works fine in version 1.3.1 but after upgrading to version 1.74 the custom converter cant get the Json to deserialize properly. I set a breakpoint on the converter put it never hits the ReadJson Method, instead it jumps out on the line
public override bool CanConvert(Type objectType) => false;
Is anyone familiar with JsonApiSerializer or customer converters able to see what the issue might be.
I receive this error.
JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.String' because the type requires a JSON primitive value (e.g. string, number, boolean, null) to deserialize correctly. To fix this error either change the JSON to a JSON primitive value (e.g. string, number, boolean, null) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List<T>) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object. Path 'data[0].relationships.matches.data[0].type'.
Object I'm trying to deserialize to
public class PubgPlayer : PubgShardedEntity
{
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty]
public DateTime CreatedAt { get; set; }
[JsonProperty]
public string PatchVersion { get; set; }
[JsonProperty]
public string TitleId { get; set; }
[JsonProperty("matches")]
[JsonConverter(typeof(RelationshipIdConverter))]
public IEnumerable<string> MatchIds { get; set; }
}
This is the custom converter
public class RelationshipIdConverter : JsonConverter
{
public override bool CanConvert(Type objectType) => false;
public override bool CanWrite => false;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//if the reader is not reading a relationship object just deserialize as normal.
//This allows us to serialize and deserialize multiple times after converting from the Json-API format
if (reader.TokenType != JsonToken.StartObject)
return serializer.Deserialize(reader, objectType);
JToken jt = JToken.Load(reader);
var dataToken = jt.SelectToken("data");
if (objectType == typeof(string))
return dataToken["id"].ToString();
return dataToken.Select(x => (string)x["id"]).ToList();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { }
}
Example Json
{
"data":[
{
"type":"player",
"id":"account.f96f1d482a5d4a21a4135f598f5a5c6a",
"attributes":{
"name":"Nomad_o7",
"stats":null,
"titleId":"pubg",
"shardId":"steam",
"patchVersion":""
},
"relationships":{
"assets":{
"data":[
]
},
"matches":{
"data":[
{
"type":"match",
"id":"e14ca749-3c19-49dd-9486-03de90669fa3"
},
{
"type":"match",
"id":"73dbca3e-eeee-44eb-9c43-3f71df0e02ae"
},
{
"type":"match",
"id":"e9f80ed0-1e78-4500-950f-174028fb73cc"
}
]
}
},
"links":{
"self":"https://api.pubg.com/shards/steam/players/account.f96f1d482a5d4a21a4135f598f5a5c6a",
"schema":""
}
},
{
"type":"player",
"id":"account.82bad0072f31455d8d9f8d834da2f2f3",
"attributes":{
"name":"TGLTN",
"stats":null,
"titleId":"pubg",
"shardId":"steam",
"patchVersion":""
},
"relationships":{
"assets":{
"data":[
]
},
"matches":{
"data":[
{
"type":"match",
"id":"4a6f064e-c292-4e9b-a3a6-bbaed891ce27"
},
{
"type":"match",
"id":"efd9434c-4116-4b56-815d-e30903b37c23"
},
{
"type":"match",
"id":"c1e56bff-db4c-470b-bb44-3c2068e33245"
},
{
"type":"match",
"id":"44d27efc-3cc6-4935-8bbe-702e4da44744"
},
{
"type":"match",
"id":"be3ebed3-9183-47f1-b4b0-40a2229dd5ff"
}
]
}
},
"links":{
"self":"https://api.pubg.com/shards/steam/players/account.82bad0072f31455d8d9f8d834da2f2f3",
"schema":""
}
}
],
"links":{
"self":"https://api.pubg.com/shards/steam/players?filter[playerNames]=Nomad_o7,TGLTN"
},
"meta":{
}
}
I don't know how it could work before since you have a bug in your class
[JsonProperty("matches")]
[JsonConverter(typeof(RelationshipIdConverter))]
public IEnumerable<string> MatchIds { get; set; }
but if you look at your json , you will see, that mathes is not a collection of strings, matches is a collection of objects
public partial class Relationships
{
[JsonProperty("assets")]
public Assets Assets { get; set; }
[JsonProperty("matches")]
public Matches MatchIds { get; set; }
}
public partial class Matches
{
[JsonProperty("data")]
public List<MatchesDatum> Data { get; set; }
}
public partial class MatchesDatum
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("id")]
public Guid Id { get; set; }
}
and IMHO you don't need any custom converter
I have the following JSON in the request body:
{
"tableName": "ApiTestTbl",
"columnsValues": {
"test1": "value1",
"column2": "value2"
}
}
And I have these 2 classes:
public class InsertIntoDbEntity
{
public string tableName { get; set; }
public InsertIntoDbColumnsValuesEntity columnsValues { get; set; }
}
public class InsertIntoDbColumnsValuesEntity
{
public Dictionary<string, string> columnsValues { get; set; }
}
And the controller function is the following:
[HttpPost]
[Route("app/singleinsert")]
public CrudReturnEntity InsertIntoDB(InsertIntoDbEntity entity)
{
return null;
}
The value of tableName is well assigned "ApiTestTbl", but columnsValues is always null.
I am using a Dictionary because the number of key-value pairs in columnValues is not defined, so I want to store them in a Dictionary for processing.
Thanks!
As #Knoop suggested in the comments, it's rather straightforward in your case, to have the Dictionary<string,string> directly as a member of your InsertIntoDbEntity model.
However in cases where you do not have this feasibility and have the requirement for a strict structure, following is the way to solve it. We are going to build a custom Json Converter for the inner property, making use of Newtonsoft.Json.
Creating a custom JSON converter for inner property
public class MyInternalConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
throw new NotImplementedException();
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if(reader.TokenType == JsonToken.StartObject)
{
JObject item = JObject.Load(reader);
// You may want to perform any sanity checks you want here
Dictionary<string, string> blah = JsonConvert.DeserializeObject<Dictionary<string, string>>(item.ToString());
return new InsertIntoDbColumnsValuesEntity
{
columnsValues = blah
};
}
return null;
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
You also need to decorate the property within your model to leverage the custom converter written. This is done as shown below
public class InsertIntoDbEntity
{
public string tableName { get; set; }
[JsonConverter(typeof(MyInternalConverter))]
public InsertIntoDbColumnsValuesEntity columnsValues { get; set; }
}
Essentially, the same technique can be leveraged at the property level or class level depending on the need for a custom deserialization.
I have complicated JSON respone which containt about 100 attributes/objects/arrays and have difrrent responses in terms of object/array.
Firstly I have structure like that (when object exists)
{
'att1': 'desc',
'att2': '83482',
'att3': null,
'test': {
'object_array1': [
100
],
'object_array2': [
'desc'
]
}
}
public class Root
{
//fields here
public Test test { get; set; }
}
public class Test
{
public List<int> object_array1 { get; set; }
public List<string> object_array2 { get; set; }
}
The issue I have is when this objects is empty.
After that resposne is changing and returning empty array.
So it is chaning to this:
{
'att1': 'desc',
'att2': '83482',
'att3': null,
'test': [
]
}
And beacause of that I have standard error:
Additional information: Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'Test' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.
I was trying to write customconverter with something like that:
private bool IsArray(string fieldName, JObject jObject)
{
return jObject[fieldName].Type == JTokenType.Array;
}
I'm using JSON.NET
But I failed miserably. Any help would be much appreciated
Use custom converter which would check token type that has been read in the ReadJson method and substitute some default value for test when the token is of type JArray (assuming it can only be an array when the test object is "empty"):
public class Root
{
[JsonConverter(typeof(TestIgnoreEmptyListConverter))]
public Test test { get; set; }
}
// .................
public class TestIgnoreEmptyListConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(Test).IsAssignableFrom(objectType) || objectType.IsArray;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var token = JToken.Load(reader);
if (token is JArray)
return default(Test);
else
return token.ToObject<Test>();
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Demo: https://dotnetfiddle.net/Q3S5hX
Your JSON propert name does not match object's name. Serializer tries to serialize/deserialize using property name or attributes.
Change this to the right name
public Test test { get; set; }
Or use attribute to set json name for a property.
[JsonProperty("test")]
public Test objects { get; set; }
Or even better, refactor your code to respect C# conventions. (I suggest to rename fields to be more descriptive)
public class Root
{
[JsonProperty("test")]
public Test Test { get; set; }
}
public class Test
{
[JsonProperty("object_array1")]
public List<int> IntArray { get; set; }
[JsonProperty("object_array2")]
public List<string> StringArray { get; set; }
}
I have the following Json Data:
{"status": "ok",
"data": [
{"2016-12-12 02:00:00": 29.6},
{"2016-12-12 02:30:00": 29.4},
{"2016-12-12 03:00:00": 28.9},
...... many more records
]}
In and ideal world, the data would include column/field names:
{"status": "ok",
"data": [
{"ts": "2016-12-12 02:00:00", "temp": 29.6},
{"ts": "2016-12-12 02:30:00", "temp": 29.4},
]}
However the column names are missing. I would like to know how to retrieve the data into the following Class Structure:
#region JSON Class
public class telemetryData
{
public string ts { get; set; }
public double temp { get; set; }
}
public class RootObject
{
public string status { get; set; }
public List<telemetryData> data { get; set; }
}
#endregion
I am deserialising the data using the following:
JavaScriptSerializer sr = new JavaScriptSerializer();
jsonResponse = sr.Deserialize<RootObject>(jsonString);
foreach (var item in jsonResponse.data)
{
OutputMoistureBuffer.AddRow();
OutputMoistureBuffer.ts = item.ts;
OutputMoistureBuffer.temp = item.temp;
}
This code is being used in an SQL Server 2008 R2 SSIS Package, using VS2008 C#. I am unable to 'step through' and debug the code in a Script component, and as such I am unable to get a accurate error message as to why I am unable to get it to work.
Any assistance on the correct formatting of my JSON Class would be greatly appreciated.
You can use Json.NET package for json deserialization. It allows to write custom converters
public class TelemetryDataConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return objectType == typeof(telemetryData);
}
public override object ReadJson(JsonReader reader,
Type objectType, object existingValue, JsonSerializer serializer)
{
var obj = JObject.Load(reader);
var property = (JProperty)obj.First;
return new telemetryData {
ts = property.Name,
temp = property.Value.Value<double>()
};
}
public override void WriteJson(JsonWriter writer,
object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Then you can either put custom converter attribute to your class, or pass converter to deserializer:
[JsonConverter(typeof(TelemetryDataConverter))]
public class telemetryData
{
public string ts { get; set; }
public double temp { get; set; }
}
Deserialization will look like:
var data = JsonConvert.DeserializeObject<RootObject>(json);
NOTE: Consider to parse date strings into DateTime objects. Also consider to use PascalCase naming for classes and properties.
I have an object where I'm trying to intercept the deserialization process for two properties which are interfaces. I'm using Json Dot Net.
The issue that I'm facing is that Json Dot Net doesn't even try to convert the two IDataStore properties. In other words, 'CanConvert' is never even run for those types.
I've tried adding JsonProperty and JsonConverter attributes and still no dice.
Any insight would be appreciated.
Edit (and Answer) After being asked by Ron Beyer to post the JSON, it became clear that Json Dot Net doesn't even consider operating on a property if the source JSON doesn't have it. When adding '"SourceDataStore":{}' it tries to convert that property. I was thinking it would look at the current object type and iterate over that but there's obviously a matching process beforehand
JSON
{
"Name":"My First Definition",
"SourceDataStoreType":"SqlDataStore",
"DestinationDataStoreType":"MongoDataStore"
}
Class
internal class Definition
{
public string Name { get; set; }
public DataStoreTypes SourceDataStoreType { get; set; }
public DataStoreTypes DestinationDataStoreType { get; set; }
public IDataStore SourceDataStore { get; set; }
public IDataStore DestinationDataStore { get; set; }
public Definition()
{
}
}
Converter
public class DataStoreConverter : JsonConverter
{
public override bool CanConvert(Type objectType)
{
var b = objectType == typeof(IDataStore);
return b;
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
return new SqlDataStore();
}
public override bool CanWrite
{
get { return false; }
}
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}
Code
var definition = JsonConvert.DeserializeObject<Definition>(definitionsJson, new JsonSerializerSettings
{
Converters = new[] { new DataStoreConverter() }
});
return definition;
(Comments to an answer)
I asked for the JSON example to verify that 1) the JSON was well formatted and 2) included the properties in question.
The issue, it turns out that you figured out, is that the JSON deserializer will not attempt to run the conversion on any properties it does not find in the JSON. This is because the deserializer parses the JSON and looks for appropriate properties in the object to fill in. If the property doesn't exist in the JSON, it won't attempt to build that property from nothing.
There are two solutions to this, either you can add a null (blank) property for those in the JSON, or you can edit your object to create them when you run the constructor, something like:
internal class Definition
{
public string Name { get; set; }
public DataStoreTypes SourceDataStoreType { get; set; }
public DataStoreTypes DestinationDataStoreType { get; set; }
public IDataStore SourceDataStore { get; set; }
public IDataStore DestinationDataStore { get; set; }
public Definition()
{
var container = YourIOCContainerHere.Instance;
SourceDataStore = container.Resolve<IDataStore>();
DestinationDataStore = container.Resolve<IDataStore>();
//Or, without using IOC/DI
SourceDataStore = new SqlDataStore();
DestinationDataStore = new SqlDataStore();
}
}