Reputation: 1028
I'm using NewtonsoftJson library, I'm trying to merge two json objects. But the desired result is not usual merge, but more like add or update. For example I have the following objects:
Object 1:
{
'FirstName': 'John',
'LastName': 'Smith',
'Enabled': false,
'Roles': [ 'User' ]
}
Object 2:
{
'FirstName': 'Harry',
'Enabled': true,
'Age': 29,
'Roles': [ 'User', 'Admin' ]
}
And the result should looks like :
{
"FirstName": "Harry",
"LastName": "Smith",
"Enabled": true,
'Roles': [ 'User', 'Admin' ]
}
Without AGE field. I just want to update values for existing fields and I want to ignore new (not existing in first one) fields - in this case , field : Age. Looks like the library is not providing such behaviour.
For custom implementation it looks really hard,because of different types at least. For example in one case it could be just a number, in other case it could be array etc. Any ideas ?
Upvotes: 3
Views: 982
Reputation: 13676
You might want to deserialize Json into JObject
and as it implements IEnumerable<KeyValuePair<>>
you could simply iterate it with a foreach loop:
JObject obj;
JObject updateWith;
foreach (var token in updateWith)
{
if (obj.ContainsKey(token.Key))
{
obj[token.Key] = token.Value;
}
}
Upvotes: 0
Reputation: 1272
You can list all the properties from the first object and compare with the second. If it finds the property in the second object take the value otherwise take it from first object:
static void Main(string[] args)
{
var obj = JObject.Parse(@"{'FirstName': 'John',
'LastName': 'Smith',
'Enabled': false,
'Roles': [ 'User' ]}");
var obj2 = JObject.Parse(@"{'FirstName': 'Harry',
'Enabled': true,
'Age': 29,
'Roles': [ 'User', 'Admin' ]}");
var propertyNames = obj.Properties().Select(p => p.Name);
var result = new JObject();
foreach (var property in propertyNames)
{
JToken value;
if (!obj2.TryGetValue(property, out value))
value = obj.GetValue(property);
result.Add(property, value);
}
Console.WriteLine(result.ToString());
}
Result:
Upvotes: 2