Community Tip - Stay updated on what is happening on the PTC Community by subscribing to PTC Community Announcements. X
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
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.
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,
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.
Pai,
I opened ticket PTC Case 12850642 . There *may* (not confirmed yet) be a bug ingesting POST data in content/body which is the root of the issue I am experiencing or it still could by my service construction.
Am awaiting feedback from support on confirmation of that and guidance.
Thanks
How are you passing the JSON, in the body of the post, header value, ...?
Are you able to post the full structure of your post (url, headers, body)??
Adam,
Using Chrome POSTMAN.
PUT https://<myplatformhere>.cloud.thingworx.com/Thingworx/Things/myDataStream/Services/myLoadService?appKey=xxxxyyyyyzzzzz
Headers(1): Content-Type application/json
Body: Raw JSON(application/json).
{"testentity":"testvalue"} and so forth.
Thanks,
You must use a POST to call a service.
Also Headers...
appKey abcdefghijkl..
Content-Type application/json
Accept application/json
Body (where the name of input parameter is JSONInput)...
{"JSONInput":"{\"PropertyName\":\"TestingProp\",\"PropertyValue\":\"Test\"}"}
Thanks Adam,
The "PUT" was a typo in my original reply as am actually using POST.
However the very helpful discovery in your reply is the payload format that you suggested. I was unaware that the variable name must prefix the payload in order to tie together to the input variable:
{"JSONInput":"{\"PropertyName\":\"TestingProp\",\"PropertyValue\":\"Test\"}"}
^ ^ ^ ^ ^ ^
Once I used this format in the Chrome body it works! Thanks.
I now need to understand how to pass the JSON payload as a FILE (the Chrome exercise was just to do initial small scale manual unit testing of the ThingWorx service that will parse the json payload). Our client doesn't allow us to "reach-in" to the data source so ContentLoaders are not an option. Instead we must *PUSH* the content from the source. In this case, the source of the json payload is a RT Linux OS and I wish to use curl to post the entire file to the service for parsing. Example:
curl -H "Content-Type: application/json" --data-binary @body.json http://<myplatform>.cloud.thingworx.com/Thingworx/Things/myDataStream/Services/myDataLoad?appKey=xxyyzz
Note: (@body.json is the json file).
I have seen a couple threads that talk in and around this ability but none yet that definitively explain. But for all intents it appears it should work.
If you or anyone has done the same or similar (PUSHING a json file to the platform for parsing), please post.
Thanks,
For those wishing to do this, here is how to pass a JSON file payload from a Linux platform to a ThingWorx Service
curl in Linux creates a post with content in the body – in this case the --data option says use the contents of the specified file for the body.
curl -X POST -H "Content-Type: application/json" --data @app.json localhost:8000/Thingworx/Things/myDataStream/Services/myLoad
app.json file contains: {"JSONInput": { "PropertyName":"HellofromLinux- EMS", "PointValue":1234, "value2":""}}
The thing is a DataStream named: myDataStream which has a DataShape with “DisplayName” and “PointValue” (two fields) defined and a service named myLoad which takes JSON input and writes to the stream
The input variable for the service myLoad is named JSONInput and is defined a type JSON.
Thanks,
-- Beck