Using the JSON library, I've imported an array of objects:
dynamic serviceList = JsonConvert.DeserializeObject(listString)
I've got an array of json objects, each has the property "name".
[
{
"name":"abcd",
"properties":{
"type":"1234"
}
},
{
"name":"xyz",
"properties":{
"type":"aaaa"
}
}
]
How do I address just the object "abcd"?
You can parse your json Array using Newtonsoft.Json JArray.Parse() function.
Use FirstOrDefault() to get record where name is "abcd"
string listString = #"[{'name': 'abcd','properties': {'type': '1234'}},{'name': 'xyz', 'properties': { 'type': 'aaaa'}}]";
JArray jArray = JArray.Parse(listString);
//FirstOrDefault to get first record which satisfy the condition
var result = jArray.FirstOrDefault(x => (string)x["name"] == "abcd");
Console.WriteLine(result);
Output:
{
"name": "abcd",
"properties": {
"type": "1234"
}
}
.Net Fiddle
The right way to solve the problem is create a static Object
Public Obj1 {
public string name {get;set;}
public Properties properties {get;set;}
}
Public Properties {
public string type {get;set;}
}
Then you can deserialize the JSON into a List<Obj1>, in this way you can iterate your list and find the "name":"abcd" Object. var myobj = mylist.FirstOrDefault(x=> x.name == "abcd")
You can use the dynamic to do pretty much anything you would like it to do, so nothing would stop you from evaluating your result in the following way:
dynamic selectionList = JsonConvert.DeserializeObject( json );
foreach (var item in selectionList) {
if ( string.Equals( (string)item.name, "abcd", StringComparison.OrdinalIgnoreCase ) ) {
Console.WriteLine( item );
}
}
This would work as per your original request, but I think you are making it a lot harder on yourself than need be :)
To see how this works, you could check this dotnetfiddle
I would probably create a class based on the spec, but I am assuming that properties is a dynamic list of properties and their values, meaning you would still end up with a Dictionary<string, object> in the end
You can access it like this:
for (int i = 0; i < serviceList.Count; i++)
{
if (serviceList[i].name == "abc")
{
DoSomethingWith(serviceList[i];
break;
}
}
Edit: didn't see that you wanted the "abc" element, so modified the code accordingly.
This will get the value for you as a JObject, from which you can access the contained values:
var serviceList = JArray.Parse(listString);
var target = serviceList.Single(s => s["name"].ToString() == "abcd");
if (target != null)
{
var type = target["properties"]["type"];
// etc
}
Related
I just want to get this JSON into some kind of object. JArray and JToken are completely confusing to me.
I can create a class so that Newtonsoft knows what to map to but if you will notice the objects have the structure of: { "anAnimal": { foo: 1, bar: 2 }} and I don't know what that mapper object will look like. I'm pretty sure this should just work instantly with zero thought on my part.
var myFavoriteAnimalsJson = #"
[
{
""Dog"": {
""cuteness"": ""7.123"",
""usefulness"": ""5.2"",
}
},
{
""Cat"": {
""cuteness"": ""8.3"",
""usefulness"": ""0"",
}
}
]";
var jArray = new JArray(myFavoriteAnimalsJson);
// grab the dog object. or the cat object. HOW CUTE IS THE DOG?
With .SelectToken() to construct the JSON path query logic.
The below sample to query the first item of animals to get the object of "Dog" token and its value.
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
JArray animals = JArray.Parse(myFavoriteAnimalsJson);
var dog = animals[0].SelectToken("Dog");
Console.WriteLine(dog);
Console.WriteLine(dog["cuteness"]);
Sample Program
Output
{
"cuteness": "7.123",
"usefulness": "5.2"
}
7.123
You can deserialize it to a List<Dictionary<string, AnimalData>>
class AnimalData
{
public decimal cuteness;
public decimal usefulness;
}
var myFavoriteAnimalsJson = #"
[
{
""Dog"": {
""cuteness"": ""7.123"",
""usefulness"": ""5.2"",
}
},
{
""Cat"": {
""cuteness"": ""8.3"",
""usefulness"": ""0"",
}
}
]";
var results = JsonConvert.DeserializeObject<List<Dictionary<string, AnimalData>>>(myFavoriteAnimalsJson);
Now each list item contains a dictionary with a single key of Dog Cat...
If you start from a serialized JSON, i.e. a string, you have to parse it:
var jArray = JArray.Parse(myFavoriteAnimalsJson);
My API response will return a list of JSON objects and I need to verify the order of the list, so I write a function as follows. But I got a problem for the LINQ order by sentence, it only works when I specify the actual field, but I need pass this field name as a parameter. so something like
var expectedList = jObjList.OrderBy(x => x.parameterFieldName.ToString());
please give me some suggestions, many thanks.
public void VerifyOrderBy(string jsonString, string parameterFieldName)
{
List<dynamic> jObjList = JsonConvert.DeserializeObject<List<dynamic>>(jsonString);
var expectedList = jObjList.OrderBy(x => x.LastName.ToString());
Assert.IsTrue(expectedList.SequenceEqual(jObjList));
}
the JSON string looks like follows
[
{
"FirstName": "w3pCj",
"LastName": "mSJOV",
"IsDeleted": false
},
{
"FirstName": "rMnH7",
"LastName": "rMnH7",
"IsDeleted": false
},
{
"FirstName": "Jia",
"LastName": "Yu",
"IsDeleted": false
}
]
You can use the nameof() operator keyword like this:
jObjList.OrderBy(x => nameof(x.LastName));
UPDATE #1
Let's say we have a Person class:
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public bool IsDeleted { get; set; }
}
Let's say we have a list of people:
var people =
JsonConvert
.DeserializeObject<List<Person>>(
File.ReadAllText("data.json", Encoding.UTF8)
);
We can have a parameter that will contain the property name we want to order by:
string parameterName = nameof(Person.LastName); // or simply "LastName"
We get a reference to that property:
PropertyInfo orderByProperty =
typeof(Person)
.GetProperties()
.SingleOrDefault(property => property.Name == parameterName);
Now we can order by the selected property:
var result = people.OrderBy(person => orderByProperty.GetValue(person)).ToList();
Please note:
Of course you should check if the orderByProperty is not null. :)
This will work only on in-memory objects (LINQ-to-Objects) but not on a DB set (LINQ-to-SQL, EF)
Do not forget to add the required using statement to be able to get the PropertyInfo:
using System.Reflection;
UPDATE #2
If you have such a simple json structure and you want to use dynamic objects for ordering then you can achieve it like this:
var people =
JsonConvert
.DeserializeObject<List<dynamic>>(
File.ReadAllText("data.json", Encoding.UTF8)
);
string parameterName = "LastName";
var result =
people
.OrderBy(person =>
{
var personObject = person as JObject;
var propertyValueObject = personObject.GetValue(parameterName) as JValue;
return propertyValueObject.Value;
})
.ToList();
Although it works I would prefer UPDATE #1 solution. :)
Here is an implementation with custom comparer. This allows you to pass any property name:
public class JObjComp<T> : IComparer<T>
{
private string _field;
public JObjComp(string field)
{
_field = field;
}
int IComparer<T>.Compare(T a, T b)
{
//this is bit flimsy but as we know that passed object is
//a dynamic, should work
dynamic aa=(dynamic)a;
dynamic bb=(dynamic)b;
return string.Compare(aa[_field].ToString(), bb[_field].ToString());
}
}
Now use our custom comparer:
List<dynamic> jObjList = JsonConvert.DeserializeObject<List<dynamic>>(jstr);
jObjList.Sort(new JObjComp<dynamic>(field));
The list is sorted insitu, so you can assert using jObjList itself.
I need to convert JSON data that I get from a REST API and convert them to CSV for some analytic. The problem is that the JSON data do not necessarily follow the same content, so I can't define a type for mapping. This has become a challenge that is taking too much of my time. I have already created some code, but of course it is not working as it throws exception on this line
var data = JsonConvert.DeserializeObject<List<object>>(jsonData);
The error is:
Additional information: Cannot deserialize the current JSON object
(e.g. {"name":"value"}) into type
'System.Collections.Generic.List`1[System.Object]' because the type
requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) 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) 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', line 2, position 10.
please let me know what I can do to get this going.
A sample of data would be like this, the fields of data can change very often, for example a new field can be added the next day, so I don't have the liberty to create a .Net class to map the data.
{
"data": [
{
"ID": "5367ab140026875f70677ab277501bfa",
"name": "Happiness Initiatives - Flow of Communication/Process & Efficiency",
"objCode": "PROJ",
"percentComplete": 100.0,
"plannedCompletionDate": "2014-08-22T17:00:00:000-0400",
"plannedStartDate": "2014-05-05T09:00:00:000-0400",
"priority": 1,
"projectedCompletionDate": "2014-12-05T08:10:21:555-0500",
"status": "CPL"
},
{
"ID": "555f452900c8b845238716dd033cf71b",
"name": "UX Personalization Think Tank and Product Strategy",
"objCode": "PROJ",
"percentComplete": 0.0,
"plannedCompletionDate": "2015-12-01T09:00:00:000-0500",
"plannedStartDate": "2015-05-22T09:00:00:000-0400",
"priority": 1,
"projectedCompletionDate": "2016-01-04T09:00:00:000-0500",
"status": "APR"
},
{
"ID": "528b92020051ab208aef09a4740b1fe9",
"name": "SCL Health System - full Sitecore implementation (Task groups with SOW totals in Planned hours - do not bill time here)",
"objCode": "PROJ",
"percentComplete": 100.0,
"plannedCompletionDate": "2016-04-08T17:00:00:000-0400",
"plannedStartDate": "2013-11-04T09:00:00:000-0500",
"priority": 1,
"projectedCompletionDate": "2013-12-12T22:30:00:000-0500",
"status": "CPL"
}
]
}
namespace BusinessLogic
{
public class JsonToCsv
{
public string ToCsv(string jsonData, string datasetName)
{
var data = JsonConvert.DeserializeObject<List<object>>(jsonData);
DataTable table = ToDataTable(data);
StringBuilder result = new StringBuilder();
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append(table.Columns[i].ColumnName);
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
foreach (DataRow row in table.Rows)
{
for (int i = 0; i < table.Columns.Count; i++)
{
result.Append(row[i].ToString());
result.Append(i == table.Columns.Count - 1 ? "\n" : ",");
}
}
return result.ToString().TrimEnd(new char[] {'\r', '\n'});
}
private DataTable ToDataTable<T>( IList<T> data )
{
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0 ; i < props.Count ; i++)
{
PropertyDescriptor prop = props[i];
table.Columns.Add(prop.Name, prop.PropertyType);
}
object[] values = new object[props.Count];
foreach (T item in data)
{
for (int i = 0 ; i < values.Length ; i++)
{
values[i] = props[i].GetValue(item);
}
table.Rows.Add(values);
}
return table;
}
}
}
The real issue here is that you are trying to deserialize into a List<object> but your JSON actually represents a single object containing a data property which then contains a list of objects. That is why you are getting this error. Json.Net can't deserialize a single object into a list. I think what you really want to do is define a container class like this:
class Root
{
public List<Dictionary<string, object>> Data { get; set;}
}
Then deserialize like this:
var data = JsonConvert.DeserializeObject<Root>(jsonData).Data;
You will then end up with a list of dictionaries, where each dictionary represents one item in the JSON array. The dictionary key-value pairs are the dynamic values in each item. You can then work with these as you would with any other dictionary. For example, here is how you would dump out all the data:
foreach (var dict in data)
{
foreach (var kvp in dict)
{
Console.WriteLine(kvp.Key + ": " + kvp.Value);
}
Console.WriteLine();
}
Fiddle: https://dotnetfiddle.net/6UaKhJ
What you're looking for is the dynamic type. Though unrelated, this answer contains much of the information on how you'll be able to iterate through the changing properties on your object.
You will need to add some additional work to figure out how to handle your result when it is an array versus a single object as your error shows us. However, this is a good first step for you.
Basically, a dynamic object is a Dictionary, much like how a JSON object is treated in JavaScript. You just need to iterate through each of the KeyValuePair objects within the main object and go through their properties.
var data = JsonConvert.DeserializeObject<dynamic>(jsonData);
var rows = new List<string>();
// Go through the overall object, and get each item in
// the array, or property in a single object.
foreach (KeyValuePair<string, object> item in data)
{
dynamic obj = item.Value;
var row = "";
// Perhaps add a check here to see if there are more
// properties (if it is an item in an array). If not
// then you are working with a single object, and each
// item is a property itself.
foreach (KeyValuePair<string, object> prop in obj)
{
// Very dummy way to demo adding to a CSV
string += prop.Value.ToString() + ",";
}
rows.Add(string);
}
This is far from a complete example, but we don't have enough information to go on to help you finish what you're trying to do.
You are trying to deserialize into a List but your JSON actually represents a single object containing a data property containing list of objects. That is why you are getting this error. Json.Net can't deserialize a single object into a list.
Please try this:Create a class which contain single property on datatype Object and pass this class for deserialization.
class Parent
{
public object Data { get; set;}
}
Then deserialize like this:
var output = JsonConvert.DeserializeObject<Parent>(jsonData);
Try using this class instead of Object
public class Datum
{
public string ID { get; set; }
public string name { get; set; }
public string objCode { get; set; }
public double percentComplete { get; set; }
public string plannedCompletionDate { get; set; }
public string plannedStartDate { get; set; }
public int priority { get; set; }
public string projectedCompletionDate { get; set; }
public string status { get; set; }
}
public class RootObject
{
public List<Datum> data { get; set; }
}
Change to this:
var data = JsonConvert.DeserializeObject<RootObject>(jsonData);
If your data is dynamic so try a dynamic list:
using System.Web.Script.Serialization;
JavaScriptSerializer jss = new JavaScriptSerializer();
var d=jss.Deserialize<dynamic>(str);
Since you're trying to deserialize an object type into a list type, it won't deseralize directly.
You can do this:
var data = JsonConvert.DeserializeObject<ObjectDataList>(jsonData);
var rows = new List<DeserializedData>();
foreach (dynamic item in data)
{
var newData = new DeserializedData();
foreach (dynamic prop in item)
{
var row = new KeyValuePair<string, string>
(prop.Name.ToString(), prop.Value.ToString());
newData.Add(row);
}
rows.Add(newData);
}
Here are new classes
//class for key value type data
class DeserializedData
{
List<KeyValuePair<string, string>> NewData =
new List<KeyValuePair<string, string>>();
internal void Add(KeyValuePair<string, string> row)
{
NewData.Add(row);
}
}
[DataContract]
class ObjectDataList
{
[DataMember(Name ="data")]
List<object> Data { get; set; }
public IEnumerator<object> GetEnumerator()
{
foreach (var d in Data)
{
yield return d;
}
}
}
As far as I can tell, more recent versions of Newtonsoft can actually do this now, no additional work required.
I was working with the binary version however, and this did still have the issue - I had a test where you could configure to use binary or json, and the json version worked just fine, but the binary complained about not getting an array type.
I started using the BsonDataReader for this, and was looking through the properties and methods on it to see how I could best look at the contents, and lo and behold, this had a property called:
reader.ReadRootValueAsArray
Setting this to 'true' did the trick.
I have a data model that is defined as a class in C#. I need to merge two objects using JObject.Merge, but in the case of one particular property I need to ignore empty string values from the 2nd object, similar to how you would ignore null values. Is there an existing attribute property for this, or do I somehow need to write my own?
void Main()
{
string json1 = #"{ Foo: ""foo1"", Bar: ""bar1"" }";
string json2 = #"{ Foo: ""foo2"", Bar: null }";
string json3 = #"{ Foo: ""foo3"", Bar: """" }";
var model1 = JObject.Parse(json1);
var model2 = JObject.Parse(json2);
model1.Merge(model2);
model1.Dump();
model1 = JObject.Parse(json1);
model2 = JObject.Parse(json3);
model1.Merge(model2);
model1.Dump();
}
public class Model
{
[JsonProperty("foo")]
public string Foo { get ; set; }
[JsonProperty("bar", NullValueHandling = NullValueHandling.Ignore)]
public string Bar { get ; set; }
}
Output (1): Model.Bar = "bar1"
Output (2): Model.Bar = "";
Desired output of (2): Model.Bar = "bar1"
EDIT: OK, I realized that attributes could not be applied since I needed to work with the raw json string only as input. This was mainly due to the fact that my classes had attributes with default values. Merging classes with empty values would trigger the defaults and end up overwriting the original, which is what I didn't want. I need to be able to take a partial json representation of a class and update the original. Sorry if that wasn't clear from the get-go. I'll update what I ended up doing an answer.
You could remove the properties with empty string values from the JObject you want to merge in, using the following extension methods:
public static class JsonExtensions
{
public static void RemovePropertiesByValue(this JToken root, Predicate<JValue> filter)
{
var nulls = root.DescendantsAndSelf().OfType<JValue>().Where(v => v.Parent is JProperty && filter(v)).ToList();
foreach (var value in nulls)
{
var parent = (JProperty)value.Parent;
parent.Remove();
}
}
public static IEnumerable<JToken> DescendantsAndSelf(this JToken node)
{
if (node == null)
return Enumerable.Empty<JToken>();
var container = node as JContainer;
if (container != null)
return container.DescendantsAndSelf();
else
return new [] { node };
}
}
Then use it like:
model2.RemovePropertiesByValue(v => v.Type == JTokenType.String && string.IsNullOrEmpty((string)v.Value));
Note this doesn't remove empty strings from arrays because that would mess up array indexing, and therefore merging. Do you need that also?
I manipulated the JObject dictionary keys to massage the specific entry. I feel dirty, but it works. I can't think of a "good" way to do this.
model1 = JObject.Parse(json1);
model2 = JObject.Parse(json3);
IDictionary<string, JToken> dictionary = model2;
dictionary["Bar"] = string.IsNullOrEmpty((string)dictionary["Bar"])
? null
: dictionary["Bar"];
model1.Merge(model2);
Okay, so I'm working on creating a REST-API, and in order to seed my database I use existing data in the forms of huge JSON-files. And I have a problem when it comes to deserializing one of the fields.
So the JSON looks like this:
{
"name" : "Magic 2013",
"booster" : [
"land",
"marketing",
"common",
"common",
"common",
"common",
"common",
"common",
"common",
"common",
"common",
"common",
"uncommon",
"uncommon",
"uncommon",
[
"rare",
"mythic rare"
]
]
}
And when you look at this, you can probably identify the problem as well. There's a field called booster, which is an array of strings.. but the last element is not a string. It's another array. So trying to deserialize it to a string[]-field fails.
I have to work with this format - there's no way for me to change it, so I'm going to have to figure out a smart way to solve this problem. Which is what I need help with.
Are there any way with JSON.NET that i could actually deserialize this? Some way I could do some sort of manual mapping saying that whenever I reach the inner array, I'm going to do some custom code?
I would be grateful for any help!
Thanks!
You could define the booster property as JArray:
public JArray Booster { get; set; }
This doesn't enforce a specific data type of the array. You can then loop through each element of this array (which will be a JToken) and test if it is a string value or yet another JArray and act accordingly:
foreach (JToken token in model.Booster)
{
var array = token as JArray();
if (array != null)
{
// The element is an array, so you can process its subelements here
}
else
{
// It's probably a string element
string value = token.ToObject<string>();
}
}
So this works for me:
class WhateverMyThingIsNamed
{
public string Name { get; set; }
public object[] Booster { get; set; }
public IEnumerable<string> GetBooster()
{
foreach (var o in Booster)
{
if (o is string)
{
yield return (string) o;
}
else if (o is JArray)
{
foreach (var element in (JArray) o)
{
yield return element.Value<string>();
}
}
else
{
throw new InvalidOperationException("Unexpected element");
}
}
}
}
...
var json = File.ReadAllText("json1.json");
var data = new JsonSerializer().Deserialize<WhateverMyThingIsNamed>(new JsonTextReader(new StringReader(json)));
var boosterList = data.GetBooster().ToList();
If it will always be the last element that will have the array instead of a string, you can deserializer is using
public class RootObject
{
public string name { get; set; }
public List<object> booster { get; set; }
}
and then extract element of the booster List using
var oddItem = booster[booster.Count -1];
This is assuming that its always the last element which is an array