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
Related
I have a list of data and want to convert it into string format like headers and values separated. How can I achieve this in c#?
Expected Result:
dynamic data = {
"values": [
[
5658830,
"Support And Training Services Llc",
"PAM",
"FINNESAND"
],
[
5658831,
"Training Services Llc",
"Bob",
"MCCART"
]
],
"headers": [
"ID",
"ENT_COMPANY1",
"FirstName",
"LastName"
]
}
How to convert in List into above format?
Here's a quick snippet to answer your question, in my own code. You'll still have to propogate the list of objects with whatever it is you have. Just change "object" to the name of your model.
List<string> listOfStrings = new List<string>();
List<object> listOfMyModels = new List<object>();
//fill your list of objects here with whatever it is you're converting to a string
foreach(object myObject in listOfMyModels)
{
string json = JsonConvert.SerializeObject(myObject);
listOfStrings.Add(json);
}
Now you have a list of strings that represent your objects and when needed, you can use the deserialize method to read it.
I am trying to get values with LINQ-TO-JSON in this JSON-TREE, So far it looks good until i've approached this categories element.
I want to get the value "Teknik" from this value and this is what i've tried to far without success.
Name = json["items"][i]["categories"].Children().Value<string>() ?? "Not assigned"
Name = json["items"][i]["categories"][0].Value<string>() ?? "Not assigned"
Name = json["items"][i]["categories"].First.Value<string>()
But none of these results works for me. Please help!
You didn't post the actual JSON string and the "tree" doesn't help. The JSON string in Querying JSON with LINQ could be represented with a similar tree, so I'll assume that's your source data:
string json = #"{
'channel': {
'title': 'James Newton-King',
'link': 'http://james.newtonking.com',
'description': 'James Newton-King\'s blog.',
'item': [
{
'title': 'Json.NET 1.3 + New license + Now on CodePlex',
'description': 'Annoucing the release of Json.NET 1.3, the MIT license and the source on CodePlex',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [
'Json.NET',
'CodePlex'
]
},
{
'title': 'LINQ to JSON beta',
'description': 'Annoucing LINQ to JSON',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'categories': [
'Json.NET',
'LINQ'
]
}
]
}
}";
JObject rss = JObject.Parse(json);
The example shows two ways to access values, through indexers or LINQ queries.
Using indexers, you'd only need [0] to access the first category of an item, eg:
rss["items"][i]["categories"][0];
That returns a JValue object as a JObject. You can cast it directly to string to get its value, eg:
(string)rss["items"][i]["categories"][0]
Or to a JValue, if you want to handle it as a JSON object and read the Value property, eg:
(rss["items"][i]["categories"][0] as JValue).Value;
A LINQ query wouldn't need indexers. If you wanted to retrieve the Category names you could write :
var query= from item in rss["channel"]["item"]
from category in item["categories"]
select new {Name=(string)category};
Or
var query = from category in rss["channel"]["item"].SelectMany(item=>item["categories"])
select new {Name=(string)category};
Or
var query = rss["channel"]["item"].SelectMany(item=>item["categories"])
.Select(category=>new {Name=(string)category});
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.
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.