Skip to main content
1-Visitor
July 16, 2015
Question

How to Convert JSON received from REST API to Bind to Object or variables

  • July 16, 2015
  • 1 reply
  • 8963 views

Could you please help me with converting JSON received from REST API to Bind to Object or variables, so that they can be displayed on the Mashup.

I have already tried with infotable, but it is now working as expected.

Thanks,

Prabhas

    1 reply

    1-Visitor
    July 20, 2015

    If your input parameter is defined as JSON or if you are using a Service that retrieves JSON.

    Thingworx will treat it as an Object.

    So {MyValue:23} can be retrieved as JSONObjectVariable.MyValue

    If you are passing in a JSON Array, the node array is inserted then it becomes JSONObjectVariable.array[node].MyValue

    Note: Array is NOT inserted if it is a JSON Object in which one of the nodes is an array.

    1-Visitor
    December 3, 2015

    Pai,

    This seems to be true when I test the service from ThingWorx Composer but not when testing from Chrome POSTMAN or CURL.

    For example:

    The simple JSON input is {"name":"Test Name"},

    I built a service as follows:

         The input parameter is defined as type JSON with name variable1

         In order to get the value for "name" the service references this as variable1.name.

    1. When I test using the Service's TEST button in Composer and pass the JSON {"name":"Test Name"} input variable - it works.

    2. When I test using Chrome POSTMAN and pass the JSON {"name":"Test Name"} as raw, Content-Type application/json - the variable1 is undefined.

    3. Also when I test using CURL from Linux and pass the JSON {"name":"Test Name"} - it fails with variable1 is undefined.

    Any ideas why the test would work within the ThingWorx Composer but not when called from outside?

    Thanks,

    1-Visitor
    December 3, 2015

    Might be the way the call is constructed you aren't declaring the Input parameter correctly.

    I believe there are a few posts in the community on this, search for how to call the REST API or something like that.