1

I have two json files - data.json and dataForUpdate.json

data.json:

 [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }]

dataForUpdate.json:

  [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }]

I need to update first json with values from second. How can I do it without JSON.parse and hardcodded replacing.

3 Answers3

2

Have you looked at Object.assign? You could do something like this:

var data = [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }]

var dataForUpdate =   [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }]

Object.assign(data[0], dataForUpdate[0]);

Browser compatibility

  • Chrome - 45+
  • Firefox - 34+
  • Internet Explorer - not supported
  • Edge - all versions
  • Opera - 32+
  • Safari - 9+
Ivan Vasiljevic
  • 5,478
  • 2
  • 30
  • 35
1

Without use of any method, you can iterate over json and update matching keys and call recursively for objects.

var data = {"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }
 
var dataForUpdate = {"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }
 
 var update = function(a, b) {
  for(key in a) {
    if(b[key] !== undefined) {
      if(typeof b[key] === 'object') {
        update(a[key],b[key]);
      } else {
        a[key] = b[key];
      }
    }
  }
 }
 update(data, dataForUpdate);
 console.log(data);
xitter
  • 708
  • 5
  • 15
0

If you are using jQuery, Then You can also use jQuery extend.

Like this

var data = [{"name": "test",
   "isDefault": true,
   "settings": {
     "type": "Separation",
     "scanner": {
     "brightness": 0,
     "contrast": 0,
     "threshold": 0,
     "isDuplex": false,
      },
     "image": {
     "invertImage": false,
     "autoRotate": true,
     "rotateValue": -180
     }
    }
 }];
 var update = [{"name": "other",
   "isDefault": false,
   "settings": {
     "scanner": {
     "brightness": 100,
     "contrast": 50,
      },
     "image": {
     "autoRotate": false,
     "rotateValue": 0
     }
    }
 }];
 
 $.extend(data[0],update[0]);
 console.log(data);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
R.K.Saini
  • 2,678
  • 1
  • 18
  • 25