0

I'm trying to generate a 2d JSON array using JSON object in Java. I'm trying to generate the following JSON.

Java Code..

          JSONObject root = new JSONObject();
        JSONObject c0 = new JSONObject();
        JSONObject c1 = new JSONObject();
        
        JSONObject attachment = new JSONObject();
        JSONObject payload = new JSONObject();
        JSONObject buttons = new JSONObject();

        root.put("recipient", c0);
        root.put("message", c1);

        c0.put("id", userId);
        c1.put("message", attachment);
        
        attachment.put("type", "template");
        attachment.put("payload", payload);
        
        payload.put("template_type", "button");
        payload.put("text", "What do you want to do next");
        payload.put("buttons", buttons);
        
        buttons.put("type", "web_url");
        buttons.put("url", "https://google.com");
        buttons.put("title", "show website");
        
        buttons.put("type", "postback");
        buttons.put("title", "Hi There");
        buttons.put("payload", "sample payload");

Expected JSON Output..

{
"recipient":{
"id":"USER_ID"
 },
"message":{
"attachment":{
  "type":"template",
  "payload":{
    "template_type":"button",
    "text":"What do you want to do next?",
    "buttons":[
      {
        "type":"web_url",
        "url":"https://google.com",
        "title":"Show Website"
      },
      {
        "type":"postback",
        "title":"Start Chatting",
        "payload":"Sample_PAYLOAD"
      }
     ]
    }
   }
 }
}

Current Output..

 {
"recipient":{"
id":"988459377921053"
},
"message":{
"message":{"
  payload":{
   "buttons":{
       "payload":"sample payload",
       "type":"postback",
        "title":"Hi There",
        "url":"https://google.com"
         },
      "template_type":"button",
      "text":"What do you want to do next"},
       "type":"template"
       }
      }
     }

I'm creating nested Json objects and adding them from the outer level to the inner level still the output JSON is not as expected. Can't understand where I'm going wrong.

Edit 1:

Tried changes mentioned by user @user1802604 but the JSON being generated is of the following format..

{  
"recipient":{  
  "id":"988459377921053"
},
"message":{  
  "attachment":{  
     "payload":{  
        "buttons":[  
           {  
              "payload":"sample payload",
              "type":"postback",
              "title":"Hi There",
              "url":"https://google.com"
           },
           {  
              "payload":"sample payload",
              "type":"postback",
              "title":"Hi There",
              "url":"https://google.com"
           }
        ],
        "template_type":"button",
        "text":"What do you want to do next"
     },
     "type":"template"
     }
    }
   }

The API to which I'm sending the JSON is returning response code 400 with message "Bad Request". Is there a way to preserve the order of elements??

1
  • JSONObject buttons = new JSONObject(); should be a JSONArray holding JSONObject Commented Feb 8, 2017 at 8:13

2 Answers 2

2

I think most of your codes are right. Only two errors need to be fixed.

Note that I'm not sure what library you're using for representing json, so the following codes may not fully correct. I assume that you're using org.json.

  1. c1.put("message", attachment); should be c1.put("attachment", attachment);.
  2. JSONObject buttons = new JSONObject(); should be created as JSONArray buttons = new JSONArray();. So you also have to create another json object for storing type, title, and url.

    JSONArray buttons = new JSONArray();
    
    JSONObject button1 = new JSONObject();
    button1.put("type", "web_url");
    button1.put("url", "https://google.com");
    button1.put("title", "show website");
    buttons.put(button1);
    
    JSONObject button2 = new JSONObject();
    button2.put("payload", "postback");
    button2.put("url", "Sample_PAYLOAD");
    button2.put("title", "Start Chatting");
    buttons.put(button2);
    
Sign up to request clarification or add additional context in comments.

7 Comments

As seen in the edits to the question above, I tried your changes but the JSON being generated has different order of elements due to which the API i'm sending the JSON is returning "400- Bad request" Error...Is there a way to preserve the order of various elemnts in the JSON ??
In general, json object does not keep order of keys. Only json array ensures the order of elements. I don't think the reason for Bad Request is caused by the order. Is there any clue which indicates the error?
I checked but Can't find any other reason for the error. Is there a away to enforce order of elements in json obj?? If not then what is the alternative?
If you really want to test it with exactly the same order, I think you should just write a json string by your hand, and send it to the API.
I thought of doing that but the url and the messages fields are dynamic i.e they will be variables hence thought of doing this..
|
0

This code is settings value of a JSONObject

    buttons.put("type", "web_url");
    buttons.put("url", "https://google.com");
    buttons.put("title", "show website");

    buttons.put("type", "postback");
    buttons.put("title", "Hi There");
    buttons.put("payload", "sample payload");

Since there is twice each key, you override the first value.

What you want is to add a JSONObject instance into a JSONArray buttons.

JSONArray arrayButton= new JSONArray();

JSONObject button.put("type", "web_url");
button.put("url", "https://google.com");
button.put("title", "show website");
arrayButton.put(button);

button.put("type", "postback");
button.put("title", "Hi There");
button.put("payload", "sample payload");
arrayButton.put(button);

To add each object into an array. Then simply add the array you have build.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.