9

I'm having an issue that I cant fix, I seek and seek in different places, but I still without find it.

See this code:

//I have 2 json to merge  

var json = @"{'client': {'name': 'Should NO CHANGE', 'lastname': '', 'email': 'asd@asd.com', 'phone': '',  'birthday': '', 'married': false, 'spouse': {'name': '', 'lastname': ''} } }";

var json2 = @" {'client': {'name': 'aaaa', 'lastname': 'bbbb', 'email': 'cccccc@ccccc.com',  'phone': '', 'birthday': '', 'married': false,  'spouse': {'name': 'dddddd', 'lastname': 'eeeee'} } } ";

//for example to properties to replace
var path = "client.spouse";
var path2 = "client.email";

dynamic o1 = JObject.Parse(json);
dynamic o2 = JObject.Parse(json2);



//I want this, but using the string (LIKE REFLECTION) 
// NOT THE DYNAMIC object 
// Can be a simple property or a complex object
o1.client.spouse = o2.client.spouse;
o1.client.email = o2.client.email; 

I need to use a string instead of "o1.client.email" to replace the content. Because can be anything. (also the json can be anything)

I can replace by strings, by dynamic, or whatever it work.

(XML works, but I lost the data type when is a date, boolean or numeric)

Example in NetFiddle.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
Math
  • 768
  • 1
  • 8
  • 18
  • Does `String.Replace` work? – wingerse Oct 09 '15 at 18:51
  • @EmpereurAiman it is well known that [String replace does not work](http://stackoverflow.com/questions/13277667/c-sharp-string-replace-does-not-work) :) – Alexei Levenkov Oct 09 '15 at 18:51
  • I don't understand the question well, but if you want to replace `client.email` with a string, `o1.client.email = "yourString";` works. – wingerse Oct 09 '15 at 18:56
  • `o1["client"]["email"]`? [JObject](http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_Linq_JObject_Item_1.htm) and [JToken](http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_Linq_JToken_Item.htm). – crashmstr Oct 09 '15 at 18:57
  • No, is not the idea, can be any object, not only a string – Math Oct 09 '15 at 18:59
  • o1["client"]["email"] for this, I dont know how many levels or object can I have. – Math Oct 09 '15 at 19:00
  • Your english is very hard to understand. I hope you don't take this negatively as it's still impressive for non native speaker. – Ian Newson Oct 09 '15 at 19:22
  • 2
    Are you looking for `o1.client.email = o2.SelectToken("client.email");` ? – Brian Rogers Oct 09 '15 at 19:32
  • close, but o1.client.email should be a string like o1["client.email"] – Math Oct 09 '15 at 19:36
  • So you want something like xpath but for Json instead of xml? – cslecours Oct 09 '15 at 19:40
  • Exactly that. Its possible ? – Math Oct 09 '15 at 19:49
  • [`SelectToken`](http://www.newtonsoft.com/json/help/html/QueryJsonSelectTokenJsonPath.htm) and [`SelectTokens`](http://www.newtonsoft.com/json/help/html/M_Newtonsoft_Json_Linq_JToken_SelectTokens.htm) support [JSONPath queries](http://goessner.net/articles/JsonPath/), I assume that's why @BrianRogers suggested it. – dbc Oct 09 '15 at 22:09

1 Answers1

32

You can use SelectToken to select any item in the JSON object hierarchy. It supports JSONPath query syntax. It returns a JToken corresponding to the value of the selected item, or null if not found. That JToken in turn has a Replace(JToken replacement) method. Thus you can do:

var o1 = JObject.Parse(json);
var o2 = JObject.Parse(json2);

var path = "client.spouse";
o1.SelectToken(path).Replace(o2.SelectToken(path));

var path2 = "client.email";
o1.SelectToken(path2).Replace(o2.SelectToken(path2));
dbc
  • 104,963
  • 20
  • 228
  • 340