JSON - Create JSONObject with array - java

I need to create in a Java (Android) a JSONObject/JSONArray with the exact following structure:
{
"listId":
[
"c02bc683-fcd7-47a5-b157-853e26ed099e",
"f8e1c9d7-ae45-4433-a315-726c1d912d09"
]
}
Can somebody help me on this please?
EDIT:
What i have is this:
JSONArray obj = new JSONArray();
JSONObject jsonObject = new JSONObject();
jsonObject.put("listId", folderId);
obj.put(jsonObject);
JSONObject json = new JSONObject();
json.put("id", obj);
But this produces something like:
{
"listId":
[
{"id":"c02bc683-fcd7-47a5-b157-853e26ed099e"},
{"id":"f8e1c9d7-ae45-4433-a315-726c1d912d09"}
]
}
Thanks

You've got your array creation a bit mixed up. What you really want is an object holding an array of string, but what you're doing is creating an array of JSONObject.
Try this instead:
String[] arr = { "c02bc683-fcd7-47a5-b157-853e26ed099e", "f8e1c9d7-ae45-4433-a315-726c1d912d09" };
JSONArray jsonArray = new JSONArray(arr);
JSONObject json = new JSONObject();
json.put("listId", jsonArray);
System.out.println(json); // {"listId":["c02bc683-fcd7-47a5-b157-853e26ed099e","f8e1c9d7-ae45-4433-a315-726c1d912d09"]}

Related

Unable to create same parameter value twice in json array

In this code, I am trying to creating something like this-
public String KLYA_JSON_LookUp_MultiNode(String KLYA_To,String KLYA_DLRURL,String KLYA_To2) {
JSONObject jsonObj = new JSONObject();
jsonObj.put("dlrurl", KLYA_DLRURL);
JSONArray array = new JSONArray();
JSONObject Array_item = new JSONObject();
Array_item.put("to", KLYA_To);
Array_item.put("to", KLYA_To2);
array.add(Array_item);
jsonObj.put("lookup", array);
CreatedJson = jsonObj.toString();
System.out.println(CreatedJson);
return CreatedJson ;
}
Output:
{"lookup": [{
"to": "890XXXXXXX"
}, {
"to": "890XXXXXXX"
}], "dlrurl": "http://www.example.com/dlr.php/......"
}
but I dont get as per the above comment, it ends up printing only one to in the array where as it should print two.
Insert
array.add(Array_item);
Array_item = new JSONObject();
between the two calls to Array_item.put.
So after Thomas and Petter's suggestion, I worked on it and this is how it got resolved.
JSONObject jsonObj = new JSONObject();
JSONArray array = new JSONArray();
JSONObject Array_item = new JSONObject();
JSONObject NextArray_item = new JSONObject();
jsonObj.put("dlrurl", "url");
NextArray_item.put("to","XXXX");
Array_item.put("to", "XXX");
array.add(Array_item);
array.add(NextArray_item);
jsonObj.put("lookup", array);
CreatedJson = jsonObj.toString();
System.out.println(CreatedJson);

Nested array for org.json

I tried to build the nested array for the following json blob using jsonobject and jsonarray. For some reason, the object is failing. Any thoughts - please, how best we can put this format.
{
"reporter_desc": "pot",
"timestamp": "1487830751",
"incidents": [
{
"src_ip": 822382162,
"exploit": "scan",
"incident_type": 3,
"occurred": "1487830751",
"description": "pot scan"
}
Here is the JSONObject and JSONArray :
JSONObject object = new JSONObject();
object.put("reporter_desc", "mtpot");
object.put("timestamp", "1487830751");
JSONArray array = new JSONArray();
JSONObject arrayElement = new JSONObject();
arrayElement.put("src_ip", 822382162);
arrayElement.put("exploit", "scan");
arrayElement.put("incident_type", 3);
arrayElement.put("occurred", "1487830751");
arrayElement.put("description", "pot vulnerability scan");
array.put("incidents");
object.put("incidents", array);

converting JSON OBJECT to JSON ARRAY

This is very simple one but struggeling. help me out of this.
I am having a json data { "abc":"test","bed":"cot","help":"me"}
I want to convert above jsonObject into JSON ARRAY like [{ "abc":"test","bed":"cot","help":"me"}]
JSONObject obj= new JSONObject(str.toString());
Iterator x = obj.keys();
JSONArray jsonArray = new JSONArray();
Map<String, String> map = new HashMap<String, String>();
while (x.hasNext()) {
for(int i=0;i<obj.length();i++) {
LOG.info("=============");
String key = (String) x.next();
jsonArray.put(obj.get(key));
}
}
I am getting only values. Please help me solve this.
Directly put JsonObject i.e. obj into jsonArray
jsonArray.put(obj);
//result is [{ "abc":"test","bed":"cot","help":"me"}]
Final code
JSONObject obj= new JSONObject(str);
JSONArray jsonArray = new JSONArray();
//simply put obj into jsonArray
jsonArray.put(obj);
//result is [{ "abc":"test","bed":"cot","help":"me"}]
It works for me.
JSONObject obj = new JSONObject(result.toString());
JSONArray arr = obj.getJSONArray("value");

add array to json file with no key

I have to make a file in JSON format which must look like the following:
xyz.json
[
{
"imagelist": "/oracle/public/oel6",
"label": "test_higgs",
"shape": "small",
"name" : "/Compute-computecli1/computeadmin/",
"networking" : {
"eth0" : {
"ipnetwork" : "/Compute-computecli1/computeadmin/ipnet"
}
}
}
]
The array should be added in the JSON file without {}, and these curly brackets have to come inside the JSON array.
The code for
{
instances:[
{
"imagelist": "/oracle/public/oel6",
"label": "test_higgs",
"shape": "small",
"name" : "/Compute-computecli1/computeadmin/",
"networking" : {
"eth0" : {
"ipnetwork" : "/Compute-computecli1/computeadmin/ipnet"
}
}
}
]
}
is:
This code adds json array as a value to "instance" key, but I want to add json array without json key.
JsonObject ipnetwork = new JsonObject();
ipnetwork.addProperty("ipnetwork", ipNetworkName);
JsonObject interface_type = new JsonObject();
interface_type.add("eth0", ipnetwork);
JsonObject instance = new JsonObject();
instance.addProperty(imageListCmdText, "/oracle/public/oel6");
instance.addProperty("label","test_higgs");
instance.addProperty("shape","small");
instance.addProperty("name","/"+customerName);
instance.add("networking",interface_type);
JsonArray instances = new JsonArray();
instances.add(instance);
JsonObject launch_plan = new JsonObject();
launch_plan.add("instances", instances);
Please tell how does this code has to be changed in order to get the output asked above.
JsonObject launch_plan = new JsonObject();
launch_plan.add("instances", instances);
These two lines create the JSON object with curly braces. You don't need them, you can just remove them and use instances, which doesn't have curly braces as it's a json array and not a json object.
JsonObject ipnetwork = new JsonObject();
ipnetwork.addProperty("ipnetwork", ipNetworkName);
JsonObject interface_type = new JsonObject();
interface_type.add("eth0", ipnetwork);
JsonObject instance = new JsonObject();
instance.addProperty(imageListCmdText, "/oracle/public/oel6");
instance.addProperty("label","test_higgs");
instance.addProperty("shape","small");
instance.addProperty("name","/"+customerName);
instance.add("networking",interface_type);
JsonArray instances = new JsonArray();
instances.add(instance);
// not needed
//JsonObject launch_plan = new JsonObject();
//launch_plan.add("instances", instances);

Convert Json array "[{}]" to "[]" in Java

I want to convert my Blank Json array to the Null Json array.
For ex., My Json array is like "[{}]" and if I got this array then automatically converts to "[]".
My code is like as below :
JsonObject jo = FetchData.getAllItemsAvg(request.getParameter("where"), request.getParameter("lastNum"),request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
ja.add(jo); // Some times ja like "[{}]" .
Check if the object is empty before adding it to the array.
(assuming you're using JsonObject):
JsonObject jo = FetchData.getAllItemsAvg(
request.getParameter("where"),
request.getParameter("lastNum"),
request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
if(!jo.isEmpty()){
ja.add(jo);
}
For com.google.gson.JsonObject:
JsonObject jo = FetchData.getAllItemsAvg(
request.getParameter("where"),
request.getParameter("lastNum"),
request.getParameter("limitAvgNum"));
JsonArray ja = new JsonArray();
if(!jo.entrySet().isEmpty()){
ja.add(jo);
}

Categories