0

How can I compare the contents of two JSON files in Angular and achieve the following result:

Let's say that I have a default.json file that looks like this:

default.json

 {
  "name": "John"
  "age": 30,
  "car1": "Ford"
  "car2": "BMW"
  "car3": "Fiat"
 }

And another specific.json file that looks like this:

specific.json

 {
  "name": "Doe",
  "car1": "Tesla",
  "car4": "Mercedes"
 }

The default.json contains always all possible keys and the specific.json the keys, which should be overwritten and additional keys, which are not contained in the default.json (in my example: "car4").

Now the following should be checked by the comparison:

If a specific key (in my example "name" and "car1") has a different value in the specific.json than in the default.json for the same keys, then these values ​​should be overwritten and used. Otherwise always the default values ​​from the default.json and the additional values ​​from the specific.json of course (in my example "car4").

At the end both files should be used, so the specific.json serves only as an update or extension of the default.json. I need this functionality in an Angular app, where I have to compare two JSON files and then use the ngx-translate library to provide specific translations based on specific business cases of the application. Check also my question about this topic please if you want: Translations based on specific keys in custom JSON files and business cases with ngx-translate (Angular 7)

I hope I could explain it well :)

Codehan25
  • 2,704
  • 10
  • 47
  • 94
  • 1
    How is this related to angular? – tic Feb 26 '19 at 20:25
  • 4
    `Object.assign(parsedDatafromDefault, parsedDataFromSpecific)`? – adiga Feb 26 '19 at 20:26
  • 1
    `const newObj = { ...defaultObj, ...specificObj };` – tic Feb 26 '19 at 20:26
  • @tic Because I need it in an Angular app, where I have to compare two JSON files and then use the ngx-translate library to provide specific translations based on specific business cases of the application. – Codehan25 Feb 26 '19 at 20:29
  • @adiga With this, the equal keys will be overwritten but what about the additional keys that are not in the default.json but in the specific.json? – Codehan25 Feb 26 '19 at 20:35
  • Then the `specific` property will be taken: [Object.assign()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) – adiga Feb 26 '19 at 20:36
  • Possible duplicate of [How can I get a list of the differences between two JavaScript object graphs?](https://stackoverflow.com/questions/264430/how-can-i-get-a-list-of-the-differences-between-two-javascript-object-graphs) – Heretic Monkey Feb 26 '19 at 20:37
  • Possible duplicate of [How can I merge properties of two JavaScript objects dynamically?](https://stackoverflow.com/questions/171251/how-can-i-merge-properties-of-two-javascript-objects-dynamically) – adiga Feb 26 '19 at 20:37
  • @tic I did not know that the spread operator overrides the same keys. I'll try that. – Codehan25 Feb 26 '19 at 20:40
  • 1
    @RomanticElectron That will never enter the body of the if, since the condition will always be false. – Heretic Monkey Feb 26 '19 at 20:41

2 Answers2

1

This is not related with Angular, but with JS.

Below example how to do it with objects.

let def = {
  "name": "John",
  "age": 30,
  "car1": "Ford",
  "car2": "BMW",
  "car3": "Fiat"
};

let specific = {
  "name": "Doe",
  "car1": "Tesla",
  "car4": "Mercedes"
}

function compose(def, spec) {
  return Object.assign(def, spec);
}

console.log(compose(def, specific));

Output:

/*
{ 
  name: 'Doe',
  age: 30,
  car1: 'Tesla',
  car2: 'BMW',
  car3: 'Fiat',
  car4: 'Mercedes' 
}
*/
adiga
  • 34,372
  • 9
  • 61
  • 83
Kamil Naja
  • 6,267
  • 6
  • 33
  • 47
1

Use es6 spread operator to merge (And override) objects

const defaultObj = {
  name: 'John',
  age: 30,
  car1: 'Ford',
  car2: 'BMW',
  car3: 'Fiat'
};

const specificObj = {
  name: 'Doe',
  car1: 'Tesla',
  car4: 'Mercedes'
}

console.log({ ...defaultObj, ...specificObj });
tic
  • 2,484
  • 1
  • 21
  • 33
  • I just tried it and it also seems to work with my ngx-translate solution. I will try to continue working with this solution. Thank you! – Codehan25 Feb 26 '19 at 21:24