CUTCODEDOWN
Minimalist Semantic Markup

Welcome Guest
Please Login or Register

If you have registered but not recieved your activation e-mail in a reasonable amount of time, or have issues with using the registration form, please use our Contact Form for assistance. Include both your username and the e-mail you tried to register with.

Author Topic: Merging JSON objects  (Read 194 times)

jmrker

  • Junior Member
  • *
  • Posts: 42
  • Karma: +1/-0
Merging JSON objects
« on: 11 Mar 2021, 01:14:50 am »
The following does not merge the two JSON object files correctly.

Is it because I'm trying to do a "deep" clone during the merge
or is it because I'm doing something else not permitted?

Code: [Select]
<script>
function showJson(obj) { return JSON.stringify(obj,null,2); }

const obj1 = {
 'records': [
   {
    'fname': 'Ally',
    'lname': 'Oop',
    'ssn': 1
   },
   {
    'fname': 'Betty',
    'lname': 'Boop',
    'ssn': 2
   }
 ]
};

const obj2 = {
 'records': [
   {
    'fname': 'Betty',
    'lname': 'Boop',
    'ssn': 2
   },
   {
    'fname': 'Elmer',
    'lname': 'Fudd',
    'ssn': 5
   }
 ]
};

console.log('obj1');  console.log(showJson(obj1));
console.log('obj2');  console.log(showJson(obj2));

let obj3 = { ...obj1, ...obj2 }  // merge into new object
console.log('both');  console.log(showJson(obj3));
</script>

The resulting object does not copy the 1st entry of 'obj1'
or is it just copying 'obj2' contents only?
Am I using the '...' (spread) operator incorrectly?

I am expecting a total of 3 records in the new object.

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 714
  • Karma: +132/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Merging JSON objects
« Reply #1 on: 11 Mar 2021, 09:50:01 am »
Objects can only contain one instance of a value, when you try to merge the two the .records of the second one OVERWRITES the first one. It's like the deep cloning issue dialed up to 11.

If you want to merge their .records, you need to access their .records individually.

Code: [Select]
var obj3 = {
records : obj1.records.concat(obj2.records)
};

Remember that no matter how you merge Objects, any matching properties of the latter overwrites the former. They are not merged. You want to merge properties you have to do it yourself.

Was butting my head against the wall over this limitation a couple weeks ago, trying to take a two dozen line section of code down in size. I hit six lines and could take it no further thanks to how objects can't have the same key more than once.

So I used map, and then went back to using six lines because Map is both slow and agonizing to use since it has no shorthand for declaration.
« Last Edit: 11 Mar 2021, 09:53:19 am by Jason Knight »
I'll fix every flaw, I'll break every law, I'll tear up the rulebook if that's what it takes. You will see, I will crush this cold machine.

jmrker

  • Junior Member
  • *
  • Posts: 42
  • Karma: +1/-0
Re: Merging JSON objects
« Reply #2 on: 11 Mar 2021, 11:05:34 am »
Thank you. 
Code: [Select]
var obj3 = {
records : obj1.records.concat(obj2.records)
};
That bit of code did the trick. 

However, it did not act as you described.  I thought duplicated record entries (like Betty Boop) would be overwritten.  When I run your example I get the entry twice.  Why is that?  My records look the same?



Ah Ha!  I think I just spotted the difference.  The 'records' contain two objects each which are different even though they contain a duplicated object, but the true records are different.  Looks to be my miss-understanding of how to format the original information.  I'll see if I can merge them as I initially expected which was into 3 entries instead of 4.
« Last Edit: 11 Mar 2021, 11:12:42 am by jmrker »

Jason Knight

  • Administrator
  • Hero Member
  • *****
  • Posts: 714
  • Karma: +132/-1
    • CutCodeDown -- Minimalist Semantic Markup
Re: Merging JSON objects
« Reply #3 on: 11 Mar 2021, 07:21:46 pm »
The difference is simpler than you're thinking. Your .records is an ARRAY, not an object. Concat of two arrays doesn't remove duplicates. Since the values are objects, you'd have to use a manual check to eliminate duplicates.

In general though all of this bespeaks bad underlying data structures.
I'll fix every flaw, I'll break every law, I'll tear up the rulebook if that's what it takes. You will see, I will crush this cold machine.

jmrker

  • Junior Member
  • *
  • Posts: 42
  • Karma: +1/-0
Re: Merging JSON objects
« Reply #4 on: 11 Mar 2021, 10:49:37 pm »
Could you clear my thinking of the JSON objects in original post?

Each object (obj1 and obj2) consists (key:value) of
1 key (records) pointing to a value which is an array of
2 additional objects (fname, lname, ssn)

Therefore the merging action is merging the single records of each object (obj1 and obj2)
to form 2 array elements of a sub-object (fname, lname, ssn) resulting in
the display of 4 sub-objects, one of which is a duplicated entry.  Is that true.

What I had expected was the removal of the duplicated sub-object,
but that is a unique entry when the sub-elements are taken as a whole.

Should I consider using a new Set() command to remove the duplicated sub-objects of the record?
Should I try converting the sub-objects to arrays, then use a new ...Set(obj) to remove dups
followed by a reformation of the Set array to a new object?  Or am I just so befuddled that
none of the above is making any sense? 

I guess I'm looking for the proper nomenclature to describe what actions I should attempt.


Addendum:
Perhaps I should describe what it is that I am asking.
Assume the husband has a list of friends in the JSON formatted file of 'obj1'.
The wife has a list of her friends in the file 'obj2'
I was just trying to merge the two lists into one combined list of friends
(without the duplicated friend listings)

It is just an exercise to test my understanding of JSON data manipulation with Javascript.
As demonstrated above, it appears my understanding is being sorely tested.
« Last Edit: 12 Mar 2021, 12:17:05 am by jmrker »

GrumpyYoungMan

  • Hero Member
  • *****
  • Posts: 532
  • Karma: +5/-0
    • GrumpyYoungMan
Re: Merging JSON objects
« Reply #5 on: 12 Mar 2021, 01:36:51 am »
I can not help with the problem other than say if the real use of this to to merge his and her friends it is possible that two different people could have the same name... so simply ignoring the duplicate names might cause issues?
Trying to learn a new trick to prove old dogs can learn new ones...

Total Novice have-a go Amateur Programmer - not sure that is the right thing to say... but trying to learn...

jmrker

  • Junior Member
  • *
  • Posts: 42
  • Karma: +1/-0
Re: Merging JSON objects
« Reply #6 on: 12 Mar 2021, 05:27:04 pm »
I can not help with the problem other than say if the real use of this to to merge his and her friends it is possible that two different people could have the same name... so simply ignoring the duplicate names might cause issues?

Thanks for the view, but I'm not trying to remove the common friends in each group,
just remove one (or more) of the duplicated entries in the final JSON object
leaving only one unique list of his/her combined friends.

 

SMF spam blocked by CleanTalk

Advertisement