Programmatically merging two JSON objects

I have two JSON objects created through the Google search API. The URL of these objects can be found below.

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=hello%20world&rsz=large http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q = hello% 20world & rsz = large & start = 8

As you can see, the first URL returns the first eight results, and the second returns the next eight. Instead of checking these results separately, I would like to programmatically combine them into a single JSON object and pass them as the first sixteen results.

I tried to do this with a few extremely simple JSON objects, but what Google returns is still a bit above my head, so I hope to help a bit with that.

As far as I know, this does not contradict the Google Terms of Service to combine two objects into one, only they always go through two results (what they will be). Some friends pointed me towards automated tools capable of doing such things, but I have not yet found such a tool.

I am currently working in ASP.NET, so the C # or VB.NET code is wonderful, but I am a bit independent of the language, so any help in any language would be greatly appreciated.

Can someone provide any help and / or advice on doing such a thing?

EDIT: These results will eventually be stored in the database, so any server-side methods will be fantastic, even if that means they are put right in the table for later work.

+6
json c # string-concatenation
source share
8 answers
function MergeJSON (o, ob) { for (var z in ob) { o[z] = ob[z]; } return o; } 

This is very similar to the code from Elliot, but in some conditions a little safer. It does not add functions to the object, which can lead to some syntax problems when used in environments such as Extjs or jQuery. I had a problem that this gave me syntax problems when they were used in an event listener. But loans go to Elliot, he did the job.

Use it like:

 a = {a : 1} b = {b : 2} c = {c : 3} x = MergeJSON ( a, b); x = MergeJSON ( x, c); result : x == {a : 1, b : 2, c : 3} 

Thanks elliot

+11
source share
 Object.prototype.merge = (function (ob) { var o = this; var i = 0; for (var z in ob) { if (ob.hasOwnProperty(z)) { o[z] = ob[z]; } } return o; }) var a = {a:1} var b = {b:2} var c = a.merge(b); // === {a:1,b:2} 
+4
source share

Also, if you really want the results to be processed on the server side, this article seems to give a fairly reasonable walkthrough.

+1
source share

If you are using a client-side solution (actually, actually), what about trying to β€œcombine” the function, I wrote: http://code.google.com/p/av-jslib/source/browse/js/ aV.ext.object.js # 36

+1
source share

Instead of combining the two results together, I just decided to parse them and then link the two together. In the end, there really was no need to combine them together when they could be easily combined into a database.

+1
source share

With jQuery you can do it!

 a = $.extend({a:1}, {b:2}); result: Object { a=1, b=2} 

http://api.jquery.com/jQuery.extend/

+1
source share

I'm not sure how you would combine these things completely, given that each of them has a lot of additional data besides the results themselves, but if you only need a JavaScript array containing all 16 results, this should work ...

 var responses = [GetJsonObjectFromThatUriUsingJqueryOrSomething(), GetJsonObjectFromThatOtherUriUsingJqueryOrSomething()]; // Probably want to check for success // and ensure that responses[i].responseData.results is valid. var results = []; for (var i = 0; i < responses.length; ++i) { results = results.concat(responses[i].responseData.results); } 
0
source share

To make it tidier, you can add the merge function to the JSON object. This is a solution from Johan van de Merwe based on Elliot's answer added to the actual JSON object.

 // Extend JSON object JSON.merge = function (o,ob) { for (var z in ob) { o[z] = ob[z]; } return o; } json3 = JSON.merge(json1,json2); 
0
source share

All Articles