2

so let us suppose i have a

json1 = {
"accounting" : 
{ "firstName" : "John",  
"lastName"  : "Doe",
"age"       : 23 },

                     { "firstName" : "Mary",  
                       "lastName"  : "Smith",
                        "age"      : 32 }
                 ],                            

"sales"      : 
{ "firstName" : "Sally",
"lastName"  : "Green",
"age"      : 27 },

                     { "firstName" : "Jim",   
                       "lastName"  : "Galley",
                       "age"       : 41 }
                 ] 

}


and i have made some changes to this json and stored it as a separate json

json2 = {
"accounting" : [  
{ "firstName" : "John",  
"lastName"  : "Doe",
"age"       : 23 },

                     { "firstName" : "Mary",  
                       "lastName"  : "Smith",
                        "age"      : 32 }
                     
                     { "firstName" : "Steve",  
                       "lastName"  : "John",
                        "age"      : 30 }
    
    
                 ],                            

"sales"      :
{ "firstName" : "Sally",
"lastName"  : "Green",
"age"      : 30 },

{ "firstName" : "Jim",  
"lastName"  : "Galley",
"age"       : 41 }

}

so now i want to find the difference between the two json and apply the changes to the first json as well . so basically i want to convert first into the second by only applying the changes to it

*this problem actually is part of a bigger problem that i am trying trying to solve . so the method of completely replacing the first json with second will not help(json1==json2 )

the json files i am dealing with are 800 lines with 6 to 7 levels of nesting and multiple lists of objects so to manually write a code will also not help

i would prefer the code in python and js would be fine as well. i used json diff and patch in js that made changes but at the wrong indices . and i have not tried it with a big data like mine also tried jsondiff python which gives the changes and differences but dont know how to apply changes to the original json .

your text

A B
  • 31
  • 2
  • The 2nd json you provided doesn't seems to be a valid json? – kaliiiiiiiii Jan 19 '23 at 14:02
  • 1
    "this problem actually is part of a bigger problem that i am trying trying to solve" are you sure this isn't an [XY problem](https://mywiki.wooledge.org/XyProblem)? – Xiddoc Jan 19 '23 at 14:02
  • Have you checked out : https://python-json-patch.readthedocs.io/en/latest/ – JonSG Jan 19 '23 at 14:02
  • @kaliiiiiiiii changed it. sorry while copying and editing it i made a mistake – A B Jan 19 '23 at 14:06
  • @Xiddoc i am not quite sure what you are trying to convey but . i would not mind a different method . but i can give an analogy let me say you are filling information of a student in a form and have half filled it . but i made changes to the form from backend so new users will be able to use the new template but you will have a different form you might say you can re enter information by downloading the new form . but you would have to it for 100 students .. just like we update our apps on phone rather than re-instaling the new one . i will just edit the old one . – A B Jan 19 '23 at 15:12
  • 1
    @JonSG hi , so i tried this out and it does apply the changes to the first json but i am not quite sure whether it will solve the other problem i am facing but thanks a lot it did help me solve some part of it . – A B Jan 19 '23 at 17:24

0 Answers0