Join two Jsons into a single object

Asked

Viewed 5,036 times

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 him to stay that way:

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

I tried to do using parse but I did not succeed.

5 answers

4


you can use the 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 the browsers you support have support for this method, you can check this out 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;
  };
}

3

You can create a new object that contains the properties of your JSON’s, no matter how many, and then switch 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.
  • +1 jsonRangers and jsonZord

  • 1

    @Renan , thank you for the reply

2

Just take advantage of the dynamicity of Javascript and assign the second object as property usuario 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);

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)

1

With jQuery, just use the $.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>

An important item to consider is, with Object.assign sub items are not merged using jQuery’s $.extend yes.

See this example to better understand:

$(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>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.