I have a requirement to read the json in which keys are vary and store into dictionary in C#.net. I am using Newtonsoft json. I am deserializing like below.
var inputData = JsonConvert.DeserializeObject<dynamic>(myObj)
If the keys are fixed I can do like below.
var val = inputData.Account;
But here keys are dynamic. Below is my json data.
{
"data": {
"Account": "150.80",
"Name": "XYZ",
"Description": "Some Value"
}
}
Here name value pairs may change. i.e., It may have like below also.
{
"data": {
"Cost": "154.80",
"Type": "S1234",
"Period": "Some Value"
}
}
How to access dynamic keys and store into a dictionary.
EDIT: This answer was for the previous posted question, not the newly revised question... I'm leaving it in, since it probably has some useful details.
I'd recommend using Json.NET to do this.
If the fact that the json data always has the field named data, then you can write fairly predictable code to output the contents of data into a dictionary.
Off the top of my head, I'd probably go for an anonymous type-based deserialization, kind of like this:
var json = #"{
""data"": {
""Account"": ""150.80"",
""Name"": ""XYZ"",
""Description"": ""Some Value""
}
}";
var schema = new { data = new Dictionary<string, object>()};
var result = JsonConvert.DeserializeAnonymousType(json, schema);
You'd have to reach into result.data to get that dictionary.
You could also just use JObject to pull out the values from data directly, but probably would need to parse that data into a dictionary, so it'd be sort of a double call going on, like this:
var jobj = JObject.Parse(json);
var data = jobj["data"];
var dict = JsonConvert.DeserializeObject<Dictionary<string, object>>
(data.ToString());
There's probably more ways to do this even more efficiently. Like I said, these are just ideas off the top of my head.
Related
I'm not sure of the correct terminology as not overly knowledgeable about json. I have an existing json string/object of the following fixed format (i.e. all items are at the root level and of straightforward key/value pairs, although the number of items can vary):
{
"product_id": "1777",
"license_key": "ECHOES-SILENCE-PATIENCE-AND-GRACE",
"valid_for": "365",
"status": "active",
"times_activated_max": 1
}
I need a way to add a json string of varying schema/format that is passed into a method. This object can be of any json format. It's to be added to the root. For example adding this:
{
"name":"John",
"age":30,
"cars":["Ford", "BMW", "Fiat"]
}
..will become this:
{
"product_id": "1777",
"license_key": "ECHOES-SILENCE-PATIENCE-AND-GRACE",
"valid_for": "365",
"status": "active",
"times_activated_max": 1
"name":"John",
"age":30,
"cars":["Ford", "BMW", "Fiat"]
}
I have Newtonsoft.Json at my disposal for this.
I did read this but couldn't map it to my problem.
Hope someone can help
The JSON.NET documentation has an article just for that: Merging JSON. You can use JObject.Merge to merge two different objects into one:
JObject o1 = JObject.Parse(#"{
'FirstName': 'John',
'LastName': 'Smith',
'Enabled': false,
'Roles': [ 'User' ]
}");
JObject o2 = JObject.Parse(#"{
'Enabled': true,
'Roles': [ 'User', 'Admin' ]
}");
o1.Merge(o2, new JsonMergeSettings
{
// union array values together to avoid duplicates
MergeArrayHandling = MergeArrayHandling.Union
});
What you posted is two objects. A JSON document may be text, but what it contains are arrays and objects. "Merging" means finding a way to combine those objects/arrays and produce a new array or object.
Merge is defined by JContainer, the parent of both JObject and JArray. This means you can use Merge to merge both arrays and objects.
Another option with arrays is to use Enumerable.Union to combine the contents of both arrays, and create a new one :
var array1= JArray.Parse("[1,2,3]");
var array2= JArray.Parse("[3,4,5, \"a\"]");
var array3=new JArray(array1.Union(array2));
This returns [1,2,3,4,5,"a"]
Json.net uses the JObject type to represent arbitrary JSON objects. This has a handy Merge method for combining two JObjects.
Simply:
string s1 = #"
{
""product_id"": ""1777"",
""license_key"": ""ECHOES-SILENCE-PATIENCE-AND-GRACE"",
""valid_for"": ""365"",
""status"": ""active"",
""times_activated_max"": 1
}";
string s2 = #"
{
""name"":""John"",
""age"":30,
""cars"":[""Ford"", ""BMW"", ""Fiat""]
}";
JObject o1 = JObject.Parse(s1);
JObject o2 = JObject.Parse(s2);
o1.Merge(o2);
Console.WriteLine(o1);
See it here
I have been building an application where JSON will be provided from a user API. It should read the data from the JSON using JSONPath and persist the selected portions. I am trying to do this using Json.Net (Newtonsoft). The following JSON is a sample:
{
// other properties here and different structure here
"Data": [
{
"Code": "625087",
"Name": "Customer Name",
"Email": "test#hfgidfgd.com"
},
{
"Code": "625087",
"Name": "Customer Name",
"Email": "test#hfgidfgd.com"
},
{
"Code": "625087",
"Name": "Customer Name",
"Email": "test#hfgidfgd.com"
}
],
// other properties here and different structure here
}
I would like to extract the array presented by the Data property content using JSONPath and convert it to List<Dictionary<string, object>> to manipulate in my application.
In tools like jsonpath.com the following JSONPath query works fine but with Newtonsoft it does not:
// get that json
string content = GetJson();
var jo = JObject.Parse(content);
var jsonPath = "$..Data.*";
var jsonPathResult = jo.SelectTokens(jsonPath, true /* to get error when it is not found */ );
Instead I got the exception:
Property '*' not valid on JArray.
If I do the JSONPath like this:
var jsonPath = "$..Data"; // same for just: "Data"
var jsonPathResult = jo.SelectTokens(jsonPath);
I have to loop on the result with two nested foreach, what I think it is not an elegant solution:
var result = new List<Dictionary<string, object>>();
foreach (var jsonResult in jsonPathResult)
{
foreach (var item in jsonResult)
{
var fields = JsonConvert.DeserializeObject<Dictionary<string, object>>(item.ToString());
// some adjusts on the fields dictionary will be applied here...
result.Add(fields);
}
}
Is there any way to get the result to take a single loop the only the content of Data property?
As shown in JSONPath - XPath for JSON, the syntax for an array element wildcard is [*]. Thus your code should look like:
var jsonPath = "$..Data[*]";
var result = jo.SelectTokens(jsonPath, true /* to get error when it is not found */ )
.Select(o => o.ToObject<Dictionary<string, object>>())
.ToList();
Here I am using JToken.ToObject<T>() to deserialize each array element directly to a Dictionary<string, object>> without re-serializing to a string.
Sample working .Net fiddle.
I am trying to deserialize an object dynamically but unsure what the syntax is:
The JSON looks like this:
"Id": 2750,
"Rev": 1,
"Fields": {
"System.AreaPath": "test",
"System.TeamProject": "proj",
"System.IterationPath": "Ipath",
"System.WorkItemType": "type"
}
I know they can be accessed like this:
var resultString = response.Content.ReadAsStringAsync().Result;
var jsonObject = JObject.Parse(resultString);
string ID= jsonObject["id"].ToString();
But I am not sure how to get to the values nested in Fields directly.
I know I can iterate through jsonObject["Fields"] but I want to access them via something like jsonObject["Fields\\System.AreaPath"].ToString(); or whichever is the correct syntax, if it exists that is.
I receive this JSON response (but I'm not even sure if this is valid JSON. is it?):
"{\"fields\":\"Name,ParentName,Description,StartDate,EndDate,StartMinute,EndMinute\",\"pos\":0,\"start\":0,\"totalRecords\":1001881,\"data\":[[null,\"AAEC 3400 76142\",null,\"2014-05-15T00:00:00\",\"2014-05-15T00:00:00\",840,1050],[null,\"AAEC 3400 76142\",null,\"2014-05-28T00:00:00\",\"2014-05-28T00:00:00\",840,1050],[null,\"ACCT 5400 25030\",null,\"2014-01-08T00:00:00\",\"2014-01-08T00:00:00\",1215,1290],[null,\"ACCT 5400 25030\",null,\"2014-02-19T00:00:00\",\"2014-02-19T00:00:00\",1215,1290]]}"
Is it possible to deserialize this response and access particular values using Newtonsoft JSON package, with or without the LINQ namespace? Can I access specific values from the "data" key?
Currently, my approach is to manipulate this JSON response into regular JSON, so instead of just having a "fields" key and a "data" key followed by lists of values, I end up deserializing normal JSON key/value pairs (like "Name":"null", "ParentName":"AAEC 3400 76142\", . . . ). Then I can access each object in the list's values by key.
But is it possible to access specific values keeping the response the way it came, without parsing/manipulating it, using Newtonsoft JSON package with/without methods in LINQ namespace?
This is how I'm deserializing the json string that I parse/manipulated manually into normal JSON:
var myList = JsonConvert.DeserializeObject<List<MyClass>>(json);
Then I can access values by key off of specific objects in the response by index:
String name = myList[0].Name;
But can I access the value of myList[0].Name without reformatting the JSON response into typical key:value pairs? Does Newtonsoft provide a way to access the value I want from the response the way it came?
Looks like that JSON has been double-serialized. I.e. some class was serialized as a JSON string, then that string was serialized as JSON again, causing JSON control characters including {,} and " characters to be escaped.
This is almost certainly a bug on the server side, you should try to get it fixed there. But if you cannot (for political reasons, say), you can check for this and work around it on the client side:
var token = JToken.Parse(json);
if (token.Type == JTokenType.String)
token = JToken.Parse((string)token);
var myList = token.ToObject<List<MyClass>>();
Update: your root JSON container is an object, not an array, so ToObject<List<MyClass>> won't work. The unwrapped JSON looks like:
{
"fields": "Name,ParentName,Description,StartDate,EndDate,StartMinute,EndMinute",
"pos": 0,
"start": 0,
"totalRecords": 1001881,
"data": [
[
null,
"AAEC 3400 76142",
null,
"2014-05-15T00:00:00",
"2014-05-15T00:00:00",
840,
1050
],
// More of the same
]
}
If you wanted to reformat that into a more traditional array of JSON objects, you could restructure your root JToken like so:
var token = JToken.Parse(json);
if (token.Type == JTokenType.String)
token = JToken.Parse((string)token);
var fields = (string)token.SelectToken("fields");
var fieldList = fields.Split(',');
var root = new JArray();
root.Add(token.SelectTokens("data[*]").OfType<JArray>().Select(a => new JObject(a.Zip(fieldList, (t, s) => new JProperty(s, t)))));
Debug.WriteLine(root);
With the result:
[
{
"Name": null,
"ParentName": "AAEC 3400 76142",
"Description": null,
"StartDate": "2014-05-15T00:00:00",
"EndDate": "2014-05-15T00:00:00",
"StartMinute": 840,
"EndMinute": 1050
},
// More of the same
]
Yes. It is straightforward and simple to deserialize a JSON response formatted in this way using the Newtonsoft JSON package with LINQ namespace. You don't have to manipulate the response string by hand. Set the JSON response string to a string object named "response", use JObject.Parse() to create a dynamic object "jobject", then you can access the "data" JSON key and set it to a JArray object. Then you can loop through each JToken in "jArray". Finally, get the values by their index in the JArray:
dynamic jobject = JObject.Parse(response);
JArray jArray = jobject.data;
foreach (JToken appointment in jArray)
{
parentName = appointment[1];
startMinute = appointment[5];
. . .
}
Before I get flagged for duplicate, I have the code from Dynamic json object with numerical keys working quite well now. The question with my numeric keys is that unfortunately, the JSON string I am getting is initially delimited by year, so would I use reflection to attempt to create a dynamic property on a dynamic object, and if so how? I know with a dynamic object I can't have obj["2010"] or obj[0]. In JavaScript this is no problem, just trying to get it working in C#. Ideas?
Example of JSON being returned:
{
"2010": [
{
"type": "vacation",
"alloc": "90.00"
},
Alternatively, sometimes the year is the second element as such:
I have no control over this json.
{
"year": [],
"2010": [
{
"type": "vacation",
"alloc": "0.00"
},
Maybe I'm misunderstanding your question, but here's how I'd do it:
static void Main(string[] args) {
var json = #"
{
'2010': [
{
'type': 'vacation',
'alloc': '90.00'
},
{
'type': 'something',
'alloc': '80.00'
}
]}";
var jss = new JavaScriptSerializer();
var obj = jss.Deserialize<dynamic>(json);
Console.WriteLine(obj["2010"][0]["type"]);
Console.Read();
}
Does this help?
I wrote a blog post on serializing/deserializing JSON with .NET: Quick JSON Serialization/Deserialization in C#
I have up-voted the question and JP's answer and am glad I dug around the internet to find this.
I have included a separate answer to simplify my use case for others to benefit from. The crux of it is:
dynamic myObj = JObject.Parse("<....json....>");
// The following sets give the same result
// Names (off the root)
string countryName = myObj.CountryName;
// Gives the same as
string countryName = myObj["CountryName"];
// Nested (Country capital cities off the root)
string capitalName = myObj.Capital.Name;
// Gives the same as
string capitalName = myObj["Capital"]["Name"];
// Gives the same as
string capitalName = myObj.Capital["Name"];
Now it all seems quite obvious but I just did not think of it.
Thanks again.