1

While trying to get the JSON from the body of a request, JSON.Parse jumbles the JSON elements, hence the webchat does not recognize it as an adaptive card.. Please help..

Node.JS Code:

var msgContent = {};
msgContent = getjson(function(resb){});
var msg = new builder.Message(session)
.addAttachment(msgContent);
session.endDialog(msg);

function getjson(callback){
request.JSON = true;
request.post("https://someapi.web.net",
function (error, response, body){
var resb = {};
resb = JSON.parse(body);
console.log(resb);
callback(resb);
});
};

Intended JSON:

{
"$schema": "http://adaptivecards.io/schemas/adaptive-card.json",
"contentType": "application/vnd.microsoft.card.adaptive",
"content": {
    "type": "AdaptiveCard",
    "body": [{
        "type": "ColumnSet",
        "columns": [{
                "type": "Column",
                "size": 2,
                "items": [{
                        "type": "TextBlock",
                        "text": "Tell us about yourself...",
                        "weight": "bolder",
                        "size": "large"
                    },
                    {
                        "type": "TextBlock",
                        "text": "We just need a few more details to get you booked for the trip of a lifetime!",
                        "isSubtle": true,
                        "wrap": true
                    },
                    {
                        "type": "TextBlock",
                        "text": "Don't worry, we'll never share or sell your information.",
                        "isSubtle": true,
                        "wrap": true,
                        "size": "small"
                    },
                    {
                        "type": "TextBlock",
                        "text": "Your name",
                        "wrap": true
                    },
                    {
                        "type": "Input.Text",
                        "id": "myName",
                        "placeholder": "Last, First"
                    },
                    {
                        "type": "TextBlock",
                        "text": "Your email",
                        "wrap": true
                    },
                    {
                        "type": "Input.Text",
                        "id": "myEmail",
                        "placeholder": "youremail@example.com",
                        "style": "email"
                    },
                    {
                        "type": "TextBlock",
                        "text": "Phone Number"
                    },
                    {
                        "type": "Input.Text",
                        "id": "myTel",
                        "placeholder": "xxx.xxx.xxxx",
                        "style": "tel"
                    }
                ]
            },
            {
                "type": "Column",
                "size": 1,
                "items": [{
                    "type": "Image",
                    "url": "https://upload.wikimedia.org/wikipedia/commons/b/b2/Diver_Silhouette%2C_Great_Barrier_Reef.jpg",
                    "size": "auto"
                }]
            }
        ]
    }],
    "actions": [{
        "type": "Action.Submit",
        "title": "Submit"
    }]
}

Received JSON:

{'$schema': 'http://adaptivecards.io/schemas/adaptive-card.json',
content:
{ actions: [ [Object] ],
     body: [ [Object] ],
     type: 'AdaptiveCard' },
  contentType: 'application/vnd.microsoft.card.adaptive' }
Derek Li
  • 2,921
  • 2
  • 22
  • 37
  • 1
    Use `console.log(body)` instead of `console.log(resb)`. Node will default to only outputting 2 levels deep when logging an object, but `body` is the full JSON string. – idbehold Jun 01 '17 at 16:39
  • @idbehold thank you so much for your response, but the issue is not with the [ [Object] ] part. The sequence of the elements in JSON gets jumbled. Not sure why the webchat does not recognize it if it gets jumbled. Only if the JSON is in the format as described in Intended JSON it populates the card. – Ramakrishnan S Jun 01 '17 at 17:48
  • 1
    That's… strange. In either case take a look at this: https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – idbehold Jun 01 '17 at 18:57
  • Can you share your bot code that reproduces the error? – nilsw Jun 06 '17 at 21:55
  • @NilsW, thanks for your response. for some reason it started working – Ramakrishnan S Jun 16 '17 at 17:53

1 Answers1

0

JSON objects are technically objects with unordered properties, so what you're seeing is a feature, not a bug.

See https://stackoverflow.com/a/5525820/5199616 for detailed discussion.

nilsw
  • 3,418
  • 3
  • 13
  • 22