Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine multiple JSON files into one; retrieve using jQuery/getJSON()

I have some jQuery code which retrieves content using getJSON(). There are n JSON files, which are retrieved from the server as needed:

/json-content/data0.json

/json-content/data1.json

/json-content/data2.json

etc...

Instead, I want to store all the JSON in a single file to reduce the number of HTTP requests needed to retrieve the data.

What is the best way to combine the JSON files into one? If I concatenate the JSON files together, it no longer works with getJSON().

UPDATE - clarified my question

like image 693
frankadelic Avatar asked Apr 26 '10 20:04

frankadelic


People also ask

How do I combine multiple JSON objects into one?

JSONObject to merge two JSON objects in Java. We can merge two JSON objects using the putAll() method (inherited from interface java.

What is jQuery getJSON?

jQuery getJSON() Method The getJSON() method is used to get JSON data using an AJAX HTTP GET request.


2 Answers

Either as individual attributes or as an array

{
 "data1":{stuff for data1},
 "data2":{stuff for data2},
 "data3":{stuff for data3}
}

or

{"response":[
  {stuff for data1},
  {stuff for data2},
  {stuff for data3}]}
like image 121
daveb Avatar answered Sep 21 '22 12:09

daveb


If you really want to reduce the number of HTTP requests from the client, one possible way is like this:

  1. Build a server side proxy that get JSon files from the third party data sources
  2. Merge your 3 JSon files in one single JSon file
  3. Make a single $.getJSON() call to your single JSon from client

To merge Json files, look at this post.

like image 42
systempuntoout Avatar answered Sep 20 '22 12:09

systempuntoout