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

frankadelic picture frankadelic · Apr 26, 2010 · Viewed 10.9k times · Source

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

Answer

daveb picture daveb · Apr 18, 2011

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}]}