javascript - How to mergeconcat two JSON files? - Stack Overflow

In some questions here in stackoverflow show how to merge two JSON objects from inner HTML or in a var

In some questions here in stackoverflow show how to merge two JSON objects from inner HTML or in a var but I want to merge two external JSON files or URLs with JSON response.

Here an exemple with local vars: /

var object1 = {name: "John"};
var object2 = {location: "San Jose"};

var objectA = $.extend({}, object1, object2);
//var objectB = object1.concat(object2);

console.log(objectA);

Then I will get my JSON like this or similar:

jQuery.getJson("data.json", function(data){...};

Any hint for concat my two JSONs: json1.json and json2.json? :)

In some questions here in stackoverflow show how to merge two JSON objects from inner HTML or in a var but I want to merge two external JSON files or URLs with JSON response.

Here an exemple with local vars: http://jsfiddle/qhoc/agp54/

var object1 = {name: "John"};
var object2 = {location: "San Jose"};

var objectA = $.extend({}, object1, object2);
//var objectB = object1.concat(object2);

console.log(objectA);

Then I will get my JSON like this or similar:

jQuery.getJson("data.json", function(data){...};

Any hint for concat my two JSONs: json1.json and json2.json? :)

Share Improve this question edited Nov 13, 2014 at 17:17 user1106925 asked Nov 13, 2014 at 17:12 LinsLins 891 gold badge3 silver badges8 bronze badges 7
  • You mean merge the JSON after getting those files? – Selvakumar Arumugam Commented Nov 13, 2014 at 17:14
  • 1 yeah: "with great precision and thinking about how to deal with conflicts". What does "merge" mean when you have {a:"one"} and {a:"two"}, for instance? If "a" is "latest published", then maybe you want to keep only the second value. If it's not, maybe you need the first? Or an array of both values? You can't "merge" unless you know the data's model, so that'd be task 1: find out what the JSON's model is, and validate it against that so your notion of what merging means applies. – Mike 'Pomax' Kamermans Commented Nov 13, 2014 at 17:14
  • Yes @Vega. For example I have two JSON files like: Joe, Walker Jon,Terry And other JSON like the same but I will to concat the content. – Lins Commented Nov 13, 2014 at 17:15
  • 1 to merge 2 document object you need to read them. the best way is to do something like this :var object = $.extend({}, object1, object2); that decision is very specific and you neet to do it by yourself. – ariel_556 Commented Nov 13, 2014 at 17:23
  • 1 @Lins: You're already getting the objects and parsing them by using $.getJSON, and you already know how to merge them once you get them using $.extend(), so I don't see what the problem is. Are you saying you want to convert it back to JSON when done? – user1106925 Commented Nov 13, 2014 at 18:22
 |  Show 2 more ments

2 Answers 2

Reset to default 4

You're almost there. You just need to re-serialize after you do the extend.

  var a = '{"foo": 1}';
  var b = '{"bar": 2}';

  var bined = $.extend({}, 
                         JSON.parse(a),
                         JSON.parse(b));

  var serialized = JSON.stringify(bined);

With jQuery you can merge two objects with

jQuery.getJson("data.json", function(data) {
    jQuery.getJson("data2.json", function(data2) {
        var concatenatedJson = $.extend({}, data, data2);
    });
});

So that you can of course only do, after both json objects are loaded.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745605862a4635668.html

相关推荐

  • javascript - How to mergeconcat two JSON files? - Stack Overflow

    In some questions here in stackoverflow show how to merge two JSON objects from inner HTML or in a var

    8小时前
    40

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信