13

My first JSON is:

{  
  "categoryId":"Painting",  
  "subCategoryId":"Residential",  
  "alternatives":    [1,2,3,4,5],  
  "criterias":["price","quantity","yom","company"],  
  "answers":[["1000","12343","4543","","4645646"],["12","23","34","","45"],["2014","","2000","1990","2005"],["xyz","","Abc","jkl","mno"]]  
}  

This will come from a java URL, here I am using PHP, in PHP I am calling a java URL.

My Second JSON is:

{"criterias":"Location"}

I am generating this using JQuery. How can I include the second JSON into the first JSON criterias?

T cube
  • 27
  • 1
  • 10
sreenath
  • 359
  • 2
  • 3
  • 12
  • 4
    JSON doesn't have any mechanism to reference/include JSON in other files. You manually have to edit the JSON and insert your other JSON there. Or load both JSON files with whatever language you are processing the data in, and write some custom logic to merge the data in that language. – Felix Kling Mar 06 '14 at 06:24
  • Which language you are referring? You need to convert your first json string to object and add a new property to it. – Purushotham Mar 06 '14 at 06:25
  • Now that you added the jquery tag, are you asking how to (custom) merge two objects? Then it doesn't have anything to do with JSON anymore. Where the data comes from becomes irrelevant. – Felix Kling Mar 06 '14 at 06:27
  • I am using jquery and php. Do we need to do this using ajax. – sreenath Mar 06 '14 at 06:28
  • *"Do we need to do this using ajax"* No, you can load the JSON with PHP and modify the resulting arrays/objects. TBH, your question is very vague. It's unclear whether you want to reference on JSON file from the other (which is not possible) or merge the data structures after the JSON is parsed, at which point it doesn't have anything to do with JSON anymore. – Felix Kling Mar 06 '14 at 06:29
  • First json will come from Java url, i need to add extra values to the first json and i need to send that back. How is it possible? – sreenath Mar 06 '14 at 06:29

5 Answers5

12

The simplest way to include one file in another is to use cpp (C preprocessor).

For example, if I have test.json as

{"name":"NAME",
#include "another.json"
}

and another.json as

"inner":"value"

you should be able to obtain

$ cpp -P test.json
{"name":"NAME",
"inner":"value"
}

Of course, you will need to make sure that resulting syntax is correct by properly formatting both pieces.

Ildar Rakhmanov
  • 121
  • 1
  • 2
  • 1
    Must say it is a very creative way, wouldn't say it's the most elegant, but i like the thought. – Tomer W Mar 08 '20 at 11:57
3

I believe the original question is how to merge these 2 JSON objects within javascript. Assuming that, the answer is simple.

var firstJson = {  
  "categoryId":"Painting",  
  "subCategoryId":"Residential",  
  "alternatives":    [1,2,3,4,5],  
  "criterias":["price","quantity","yom","company"],  
  "answerss":[["1000","12343","4543","","4645646"],["12","23","34","","45"],["2014","","2000","1990","2005"],["xyz","","Abc","jkl","mno"]]  
};

var secondJson = {"criterias":"Location"};

firstJson.criterias = $.extend({},firstJson.criterias,secondJson.criterias);
PressingOnAlways
  • 11,948
  • 6
  • 32
  • 59
1

You need to convert your first JSON string to object and add a new property to it.

If it is Java, you can use Google's Gson library

Gson gson = new Gson();
JsonObject jsonObj = gson.fromJson (jsonStr, JsonElement.class).getAsJsonObject();
jsonObj.add("criterias", "Location");

If it is JavaScript,

var jObj = JSON.parse(jsonString);
jObj.criterias = "Location";  //jObj.NewItem = "NewValue";
Purushotham
  • 3,770
  • 29
  • 41
1

There is a library in npm which does this in simplest way.

Check this: https://www.npmjs.com/package/coded-json

But the cost is, your target file extension should be .cjson otherwise your json schema will validate with errors.

There are other features like adding comments. Check documentation here: https://github.com/SubhenduShekhar/cjson/blob/main/README.md

-4

Here is the example of how you write nested JSON

{"menu": {
   "id": "file",
   "value": "File",
   "popup": {
       "menuitem": [
           {"value": "New", "onclick": "CreateNewDoc()"},
           {"value": "Open", "onclick": "OpenDoc()"},
           {"value": "Close", "onclick": "CloseDoc()"}
        ]
   }
 }}

For More examples visit following links:

http://www.jsonexample.com/

http://json.org/example

create nested JSON object in php?

Community
  • 1
  • 1
Jatin patil
  • 4,252
  • 1
  • 18
  • 27