Join two Jsons in a single object

3

I have the following json:

{  
   "razao_social":"INTELIDER",
   "nome_fantasia":"INTELIDER LTDA",
   "rg_insc_estadual":"123456"
}

And another json:

{  
   "usuario":{  
      "login":"gleyson",
      "senha":"987654"
   }
}

I need it to look this way:

{  
       "usuario":{  
          "login":"gleyson",
          "senha":"987654"
       },
       "razao_social":"INTELIDER",
       "nome_fantasia":"INTELIDER LTDA",
       "rg_insc_estadual":"123456"
}

I tried to use parse but I did not succeed.

    
asked by anonymous 26.06.2017 / 20:38

5 answers

2

You can use Object.assign

var obj1 = {
  "razao_social":"INTELIDER",
  "nome_fantasia":"INTELIDER LTDA",
  "rg_insc_estadual":"123456" 
};
var obj2 = { 
  "usuario":{
    "login":"gleyson",
    "senha":"987654"
  }
};

var obj = Object.assign({}, obj1, obj2);
console.log(obj);

Before using the above feature, see if all of the browsers you support supports this method, you can check this at: Mozilla MDN - Object.assign - Browser compatibility

If necessary, add the following Polyfill :

if (typeof Object.assign != 'function') {
  Object.assign = function(target, varArgs) { // .length of function is 2
    'use strict';
    if (target == null) { // TypeError if undefined or null
      throw new TypeError('Cannot convert undefined or null to object');
    }

    var to = Object(target);

    for (var index = 1; index < arguments.length; index++) {
      var nextSource = arguments[index];

      if (nextSource != null) { // Skip over if undefined or null
        for (var nextKey in nextSource) {
          // Avoid bugs when hasOwnProperty is shadowed
          if (Object.prototype.hasOwnProperty.call(nextSource, nextKey)) {
            to[nextKey] = nextSource[nextKey];
          }
        }
      }
    }
    return to;
  };
}
    
26.06.2017 / 20:48
2

Just take advantage of the dynamism of JavaScript and assign the second object as usuario property of the first object.

var obj1 = { "razao_social":"INTELIDER","nome_fantasia":"INTELIDER LTDA","rg_insc_estadual":"123456" };
var obj2 = { "usuario":{"login":"gleyson","senha":"987654"} };

obj1.usuario = obj2.usuario;

console.log(obj1);
    
26.06.2017 / 20:41
2

You can create a new object that contains the properties of your JSON's, no matter how many, and then move to JSON again.

For example:

var json1 = { "razao_social":"INTELIDER", "nome_fantasia":"INTELIDER LTDA", "rg_insc_estadual":"123456" };
var json2 = { "login":"gleyson","senha":"987654" };
var json3 = { "foo": "bar"};
var json4 = { "etc": "etc. acho que já deu pra entender" };
var json5 = { "palmeiras": { "quantidadeMundiais": "0" } };

var jsonRangers = [json1, json2, json3, json4, json5];

var jsonZord = {};
for (var i = 0; i < jsonRangers.length; i++) {
    for (var propriedade in jsonRangers[i]) {
        jsonZord[propriedade] = jsonRangers[i][propriedade];
    }
}

JSON.stringify(jsonZord); // Só pra mostrar o resultado no console.
    
26.06.2017 / 20:46
1

You can assign:

var json1 = { "razao_social":"INTELIDER","nome_fantasia":"INTELIDER LTDA","rg_insc_estadual":"123456"}


var json2 = { "usuario":{"login":"gleyson","senha":"987654"} }

json1.usuario = json2.usuario;
console.log(json1)
    
26.06.2017 / 20:42
1

With jQuery, just use $ .extend . It would look like this:

$(function() {
  foo = {
    "razao_social": "INTELIDER",
    "nome_fantasia": "INTELIDER LTDA",
    "rg_insc_estadual": "123456"
  };

  bar = {
    "usuario": {
      "login": "gleyson",
      "senha": "987654"
    }
  };

  // Comando que faz a mesclagem
  fooBar = $.extend(foo, bar);

  console.log(fooBar);

});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
  

Animportantitemtotakeintoconsiderationis,withObject.assignsubitemsarenotmerged,using$.extendofjQueryyes.

Seethisexampletobetterunderstand:

$(function() {
  obj1 = {
    nome: 'teste 1',
    subItem: {
      conta: 100
    }
  };

  obj2 = {
    nome2: 'teste 2',
    subItem: {
      conta2: 200
    }
  };

  recursivo = $.extend(true, {}, obj1, obj2);
  naoRecursivo = Object.assign({}, obj1, obj2);

  // diff
  console.log(recursivo); // possui conta e conta2
  console.log(naoRecursivo); // nao possui conta
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
26.06.2017 / 20:52