1

I have a Json object

{'name':'NARM','options':['N','A', 'P']} (da1)

and another

{'name':'NARM','options':['N','A']} (da2)

I want to find the difference, ideally I'd like to return

{'name':'NARM','options':['P']}.

I tried very simply var missing = da1.execpt(da2); It seems to be returning all of da1

Any thoughts?

1 Answer 1

2

It would be easier if you use the Except on the arrays in your json

string json1 = @"{'name':'NARM','options':['N','A', 'P']}";
string json2 = @"{'name':'NARM','options':['N','A']}";

var j1 = JObject.Parse(json1);
var j2 = JObject.Parse(json2);

var diff = j1["options"].Select(x => (string)x)
            .Except(j2["options"].Select(x => (string)x))
            .ToList();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a bunch. LINQ is pretty new to me, I've been using SQL for ever, but this new-fangled stuff is so foreign to me. Most of this I understand, but what is the purpose of (x => (string)x)? Does that just iterate over everything in "options" and return it as a string?
@spuppett Yes, just to convert JValue (Json.Net's internal representation) to string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.