Click here to Skip to main content
15,074,987 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

Is There Any Way to Two merge Two Json Object With Comparing Data According to if data duplicated so select the one where CreationTimeUtc,LastWriteTimeUtc and Length is greater than the second,

C#
if json1= [
  {
    "CreationTimeUtc": "9/20/2015 1:38:22 PM",
    "LastWriteTimeUtc": "9/20/2015 1:38:22 PM",
    "Name": "test1.txt",
    "Length": "1",
     "owner":"pc1"
  },
  {
    "CreationTimeUtc": "9/20/2015 1:38:16 PM",
    "LastWriteTimeUtc": "9/20/2015 1:38:16 PM",
    "Name": "test2.txt",
    "Length": "5" ,
    "owner":"pc1"
  }
];

json2=
[
  {
    "CreationTimeUtc": "9/20/2015 1:38:22 PM",
    "LastWriteTimeUtc": "9/21/2015 1:38:22 PM",
    "Name": "test1.txt",
    "Length": "1",
    "owner":"pc2"
  },
  {
    "CreationTimeUtc": "9/20/2015 1:38:16 PM",
    "LastWriteTimeUtc": "9/20/2015 1:38:16 PM",
    "Name": "test2.txt",
    "Length": "4" ,
    "owner":"pc2"
  }
]
result=
[
  {
    "CreationTimeUtc": "9/20/2015 1:38:22 PM",
    "LastWriteTimeUtc": "9/21/2015 1:38:22 PM",
    "Name": "test1.txt",
    "Length": "1",
    "owner":"pc2"
  },
  {
    "CreationTimeUtc": "9/20/2015 1:38:16 PM",
    "LastWriteTimeUtc": "9/20/2015 1:38:16 PM",
    "Name": "test2.txt",
    "Length": "5" ,
    "owner":"pc1"
  }
]

Thanks
Posted
Comments
Sinisa Hajnal 21-Sep-15 2:27am
   
Create objects from json, compare objects however you want and then send them on as json (single object) again.
Member 11593359 21-Sep-15 4:20am
   
thank you for your reply
i am new in c# so is there any example or solution for this
Sinisa Hajnal 21-Sep-15 5:25am
   
Yes, plenty. Google it. You have a choice of doing the comparison in javascript. If you're doing it in C# (server side) you should alredy have the objects from the model. Then you just compare properties of the objects.

1 solution

You can try this:
JavaScript
var result = json1.replace(/\]/g, ",") + json2.replace(/\[/g, "");
   
v2
Comments
Sinisa Hajnal 21-Sep-15 5:24am
   
This will just put all elements from two objects into one bigger object. As I understood the problem, OP needs to compare elements and choose which parts to keep and end up with same number of elements as the original ones? Vote 1
vagelis1 21-Sep-15 5:30am
   
sorry i missed the comparison part. I thought our friend just wanted array merge.
Given the new (for me) input our friend must do a property by property check for duplicate records. The length part is a little fade

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900