Newtonsoft Get sub key from json without indexer syntax - c#

I got a json with some nested object, for example:
{
"OrganizationData": {
"Org1": {
"Name": "Rega And Dodli",
"EmployessNum": "100000000"
},
"Org2": {
"Name": "Sami And Soso",
"EmployessNum": "2"
}
}
}
I want to get for example the value for the "Name" of "Org1".
I know I can do something like this:
var rss = JObject.Parse(mystring);
var value = rss["OrganizationData"]["Org1"]["Name"];
My question is if it's possible to replace the multiple indexers part (["OrganizationData"]["Org1"]["Name"]) with a single indexer (or something else which is not an indexer) with a single string which is composed of all 3 keys and still get the same value?
For example something like:
var rss = JObject.Parse(mystring);
var value = rss["OrganizationData:Org1:Name"];
I remember there's something with ":" but this one I tried in the example above did not work.

You can use the JObject.SelectToken method, using period (.) as the property path delimiter. For example:
var value = rss.SelectToken("OrganizationData.Org1.Name");

Related

Adding a complex object to JObject using a delimited path/key

I am working with Jsons which I don't know their structure in advanced. Just for example:
{
"OrganizationData": {
"Org1": {
"Name": "Rega And Dodli",
"EmployessNum": "100000000"
},
"Org2": {
"Name": "Sami And Soso",
"EmployessNum": "2"
}
}
}
I'm currently getting values by using the SelectToken method to which I can pass a key with a sub key like this:
var token = myJObject.SelectToken("OrganizationData.Org1")
This works fine. Now I want to add a new entry to the JSON using a string like that, something like:
myJObject.Add("OrganizationData.Org3", myValueJson);
but calling add like that directly just adds a new key to the json called "OrganizationData.Org3" and not creating a new sub key called "Org3" inside "OrganizationData" like the current "Org1" and "Org2".
How can I add a new value with a delimited string like needed?
JSON doesn't have subkeys or delimited keys. OrganizationData.Org1 is a LINQ to JSON search expression, not a subkey.
To add Org3 you can use one of the many ways available to modify a JSON object. You can add a child element to OrganizationData or a sibling to one of the other Org nodes.
To add a child element to a node, you could use .SelectToken("OrganizationData") if you don't already have a reference to it, and use JObject.Add to add the new node. You'll have to cast the result to JObject first, as SelectToken returns a JToken. If there's a chance that OrganizationData is an array, you'll have to check the type too.
For example:
var token = myJObject.SelectToken("OrganizationData");
if(token is JObject orgObj)
{
orgObj.Add("Org3",myValueJson);
}
Working with unknown paths
The same thing works if the path is specified at runtime. In this case, all that's needed is to separate the last part from the rest of the path, perhaps using String.LastIndexOf`:
var lastDot=path.LastIndexOf('.');
if (lastDot<0)
{
//Oops! There's no dot. What do we do now?
}
var parent=path.Substring(0,lastDot);
var key=path.Substring(lastDot+1);
var token = myJObject.SelectToken(parent);
if(token is JObject orgObj)
{
orgObj.Add(key,myValueJson);
}
You'll have to decide what to do if the path contains no dot. Is this an invalid path? Or should a new object be added under the root object?

Replace Json properties with NewtonSoft

No idea where to begin with this, so I don't have any sample code.
I need to change the name of a property in a json document.
var json = (#"{""id"":""12"",
""title"":""My Title"",
""Chunks"":[
{
""id"":""137"",
""title"":""Title"",
""description"":""null"",
""selections"":[
{
""id"":""169"",
""title"":""Choice"",
""sort_order"":""null"",
""questions"":[
]
}
]
}
]
}
}
}");
I need to change the "id" that's got the value of 12 to "document_id" and leave the other ids alone. Are there any C# libraries like NewtonSoft that allow you to change the property rather than the property value. Seems like a common scenario but I haven't seen anything close to what I'm trying to do. I suppose I could convert the json to a string and do a replace, but that doesn't seem very elegant.
An approach using Newtonsoft.Json.Linq.JObject would look something like:
var obj = JObject.Parse(json);
obj["document_id"] = obj["id"]; // create new property called "document_id"
obj.Remove("id"); // remove the "id" property
Console.WriteLine(obj);
Also note that your JSON is not valid. It has two extra } at the end.
Assuming you would want to replace all the keys when there could be more than one node with key as "id" and value "12", you could use Linq to identify Tokens with Key "Id" and Value "12" and then use Add/Remove methods for creating a new node with different name.
For example,
JToken node = JToken.Parse(json);
var jObjectsWithTitle = node
.SelectTokens("$..*")
.OfType<JObject>()
.Where(x => x.Property("id") != null && Convert.ToInt32(x.Property("id").Value) == 12);
foreach(var item in jObjectsWithTitle)
{
item.TryGetValue("id",out var currentValue);
item.Add("document_id",currentValue);
item.Remove("id");
}

JsonConvert.DeserializeAnonymousType definition syntax issue

I have the following code:
var definition = new { result = "", accountinformation = new[] { "" , "" , "" } };
var accountInformationResult = JsonConvert.DeserializeAnonymousType(responseBody, definition);
The account information structure comes back from an endpoint as an array with each element being another array containing 3 strings. So the embedded array is not in a key value pair format. With the above definition accountinformation returns null. What should the syntax be for this structure?
For reference this is what is going on in the php endpoint.
$account_information[] = array( $billing_company, $customer_account_number, $customer_account_manager );
This first line is in a loop. Hence the multi-dimensional array.
echo json_encode(array('result'=>$result, 'account_information'=>$account_information));
I know I could use dynamic but why the extra effort?
I assume your json will look something like this:
{
"result": "the result",
"account_information": [
["company1", "account_number1", "account_manager1"],
["company2", "account_number2", "account_manager2"]
]
}
In that case you should be able to deserialize with the following definition (note the underscore in account_information:
var definition = new { result = "", account_information = new List<string[]>() };
In json you are allowed to add extra properties as you please while your data model changes. So if you define a data model that does not include one of these properties, the property will simple be ignored. In your case the definition does not have a property called account_information (exactly), so this part of the json is ignored while deserializing.
EDIT:
If it's going to be an anonymous abject anyway, you may also consider parsing into a JObject:
var obj = JObject.Parse(responseBody);
string firstCompany = obj["account_information"][0][0];
string secondCompany = obj["account_information"][1][0];

JSON.Net getting a dynamic object

I don't think the title of this post explains what the problem is, but I didn't know how to word it.
Basically I have this response from an API of which I have no control over:
"variations":{
"1033308042319364133":{
"id":"1033308042319364133",
"order":null,
"created_at":"2015-07-20 13:45:45",
"updated_at":"2015-07-20 13:47:11",
"title":"Male",
"mod_price":"+0.00",
"modifier":1033306667720114205,
"product":0,
"difference":"+£0.00"
},
"1033308953984892967":{
"id":"1033308953984892967",
"order":null,
"created_at":"2015-07-20 13:47:34",
"updated_at":"2015-07-20 13:47:34",
"title":"Female",
"mod_price":"+0.00",
"modifier":1033306667720114205,
"product":0,
"difference":"+£0.00"
},
"1033309404260204585":{
"id":"1033309404260204585",
"order":null,
"created_at":"2015-07-20 13:48:27",
"updated_at":"2015-07-20 13:48:27",
"title":"Male (Junior)",
"mod_price":"+0.00",
"modifier":1033306667720114205,
"product":0,
"difference":"+£0.00"
},
"1033309540147265579":{
"id":"1033309540147265579",
"order":null,
"created_at":"2015-07-20 13:48:44",
"updated_at":"2015-07-20 13:48:44",
"title":"Female (Junior)",
"mod_price":"+0.00",
"modifier":1033306667720114205,
"product":0,
"difference":"+£0.00"
}
}
in my c# code I loop through variations like this:
// Get our child variants
var variations = model["variations"];
var IsNull = IsJTokenNull(variations);
var variants = !IsNull ? new List<VariationResponseModel>() : null;
// If we have some variations
if (!IsNull)
{
// Loop through our variations
foreach (var variant in variations)
{
// Add our variant to our list
variants.Add(CreateVariants(variant.First));
}
}
As you can see, I am using variant.First to select the object within the property. My question is, is this the best way to do this? It seems like an awful hack.
This looks like a .net Dictionary more than a list. If VariationResponseModel has the correct properties, you could just do:
var variants = JsonConvert.DeserializeObject<Dictionary<string, Variant>>(variations);
or using the JObject class
var variants = JObject.Parse(variations).ToObject<Dictionary<string, Variant>>();
Both approaches are equivalent, and assume that you got your input as a JSON string. If your input is already a JObject, you can just use:
var variants = variations.ToObject<Dictionary<string, Variant>>()
If you need the variants in a list/enumerable afterwards, just use variants.Values
(JsonConvert / JObject is from the Json.net deserializer)

json.net IEnumerable

I have the following json file
{"fields":[
{
"status":"active",
"external_id":"title",
"config":{},
"field_id":11848871,
"label":"Title",
"values":[
{
"value":"Test Deliverable"
}
],
"type":"text"
},{
"status":"active",
"external_id":"client-name",
"config":{},
"field_id":12144855,
"label":"Client Name",
"values":[
{
"value":"Chcuk Norris"
}
],
"type":"text"
}}
And I want to select the value of the field that has its external_id = "title" for example, I'm using Json.Net and already parsed the object. How do i do this using lambda or linq on the Json object, I trird something like this
JObject o = JObject.Parse(json);
Title = o["fields"].Select(q => q["extenral_id"].Values[0] == "title");
Which is not event correct in terms of syntax. I'm not very proficient in Lambda or Linq thought its been there for a while. Appreciate the help
Thanks
Yehia
Or you can do this:
string json = "{\"fields\":[{\"status\":\"active\",\"external_id\":\"title\",\"config\":{},\"field_id\":11848871,\"label\":\"Title\",\"values\":[{\"value\":\"Test Deliverable\"}],\"type\":\"text\"},{\"status\":\"active\",\"external_id\":\"client-name\",\"config\":{},\"field_id\":12144855,\"label\":\"Client Name\",\"values\":[{\"value\":\"Chcuk Norris\"}],\"type\":\"text\"}]}";
JObject obj = JObject.Parse(json);
JArray arr = (JArray)obj["fields"];
var externalIds = arr.Children().Select(m=>m["external_id"].Value<string>());
externalIds is a IEnumerable array of string
Or you can chain it together and select the object in one line:
var myVal = JObject.Parse(json)["fields"].Children()
.Where(w => w["external_id"].ToString() == "title")
.First();
From there you can append whatever selector you want ie if you want the external_id value then append ["external_id"].ToString() to the end of the first() selector.
Build classes for your objects first, then parse them so you can access them correctly and its no anonymous type anymore.
For example this classes:
class MyJson {
public List<MyField> fields {get;set;}
}
class MyField {
public string status {get;set;}
public string external_id {get;set;}
// and so on
}
Then use that class for parsing the json (don't know the exact syntax right now) like this:
var o = Json.Parse(json, typeof(MyJson));
And then you can select your data easily with Linq and have intellisense in VS (or similar dev env):
var myData = o.fields.Where(q=>q.external_id=="title");
If you had your JSON converted to objects (basically what Marc suggested), the LINQ query would look something like:
o.fields.Single(q => q.external_id == "title")
But if you don't want to do that, you have to access the values by string keys. If you don't want to convert the type of the value, you can simply use indexing (["key"]). But if you want to convert the type, you can use Value<Type>("key"). Putting it together, the whole query might be:
o["fields"].Single(q => q.Value<string>("external_id") == "title")

Categories

Resources