-1

Have to write a generic method which will take json of any structure and add new properties to each element with data

Structure # 1, (Person)

Input Json data,

 {'name':'sam', 'age': 12}

Expected output,

{'name':'sam', 'name_xyz': 'Rob', 'age': 12, 'age_xyz': 15}

Structure # 2, (Person with address)

Input json data,

{ 'name': 'sam', address : { 'city': 'fishers', 'zip': 23456 } }

Expected output,

{'name': 'sam', 'name_xyz': 'rob', address : { 'city': 'fishers', 'city_xyz': 'fishers', 'zip': 23456, 'zip_xyz': 678768} }

Structure # 3 (Person's)

Input json data,

[{'name': 'sam'}, {'name':'rex'}]

Expected Output,

[{'name': 'sam', 'name_xyz': 'felix'}, {'name':'rex', 'name_xyz' : 'bob'}]

I have something for defined model using NewtonSoft, but I need generic method to parse and evaluate any type of json data.

var jsonData = JsonConvert.SerializeObject(modelData);
var jArray = JArray.Parse(jsonData) as JArray;
dynamic persons = jArray;
foreach (dynamic person in persons)
{
 var name = person.name;
 var newname = Getnewname(name);
 person.Add(new JProperty("name_xyz", newname));

 var age = person.age;
 var newage = GetnewAge(age);
 person.Add(new JProperty("age_xyz", newage));
}
var result = persons.ToString();

Things to consider,

  1. Look for each node, determine if it is array or object
  2. Found object, create new object at same level with same property post fix '_xyz'
  3. Found Array, loop through each object and same step 2
  4. It will be nth level depth
  • 2
    I am not clear what the question is in this post. – John Wu Mar 01 '19 at 22:53
  • want to read json of any type/structure then add new properties to each of existing properties with naming convention 'existingproperty_xyz' with new value – user8539606 Mar 01 '19 at 22:59
  • From your Structure #2 expected output, it doesn't seem like you are creating a new object at the same level as you stated in Consideration #2. More like replacing it or just traversing into it and adding more properties. Other than that, you can achieve this using some recursive method. – alans Mar 01 '19 at 23:36
  • Yes correct not new object, just traversing into and add more new properties. I am looking for that recursive function. – user8539606 Mar 02 '19 at 00:01
  • @alans correct just traversing and adding more properties. – user8539606 Mar 02 '19 at 00:02
  • Something like this is not readily available in C# or NewtonSoft.JSON. You can write your own logic which will use reflection to identify the existing properties and create new properties. You can read about reflection [Here](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/reflection). Also you can read about using reflection on dynamic objects [here](https://stackoverflow.com/questions/2634858/how-do-i-reflect-over-the-members-of-dynamic-object) – Chetan Mar 02 '19 at 00:51
  • How do you determine what the value should be for the new properties that are added? – Brian Rogers Mar 02 '19 at 01:03
  • Out of sheer curiosity, what is the use case for this magical method? – CoolBots Mar 02 '19 at 03:32

1 Answers1

0

I have encountered a similar situation and I solved it using a "nested dictionary" approach. While it is always a pleasure to work in the comfort zone of strongly typed entities, there are times when creating C# classes might get too cumbersome. This was my situation.

Treat the entire JSON like a dictionary where one or more keys might store another Dictionary. And this might continue several levels deep. The method Jobject.Parse worked for me. The JObject works like a key-value collection and you can recursively retrieve more JObject instances as you traverse deeper.

Newtonsoft reference - flat dictionary https://www.newtonsoft.com/json/help/html/SerializingCollections.htm

This will NOT work very well with a nested structure like yours.

Newtonsoft code snippet - parsing as a JObject

string json="[{'name': 'sam', 'name_xyz': 'felix'}, {'name':'rex', 'name_xyz' : 'bob'}]"
JObject o = JObject.Parse(json);

https://www.newtonsoft.com/json/help/html/ParseJsonObject.htm

Newtonsoft reference - JObject https://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_Linq_JObject.htm

More examples on using JObject as opposed to hard typing

https://www.newtonsoft.com/json/help/html/QueryingLINQtoJSON.htm

Sau001
  • 1,451
  • 1
  • 18
  • 25