cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

GetJSON not returning value if JSON is just value response

EK_10370207
5-Regular Member

GetJSON not returning value if JSON is just value response

EK_10370207_0-1687288536849.png

I am making a API call to a 3rd party service that returns a JSON response with the content of just the value (as opposed to "key":"value" of normal JSON).  Thingworx does not seem to like this and doesn't return a usable response except for the headers.  GetText does not work as the response is automatically formatted as JSON. See attached image for format of response from Postman

ThingWorx 9.1.8-b845

10 REPLIES 10
PaiChung
22-Sapphire I
(To:EK_10370207)

Have you tried to output/cast the value that is returned from GetJSON as string to see if that works?

The output you show on the right screenshot though, ... can that even be considered a json?

EK_10370207
5-Regular Member
(To:PaiChung)

I've tried doing JSON.stringify and JSON.parse, but it contains nothing in the body. As far as considered to be JSON, technically, it can be.  It is just a string-quoted value without a key.

PaiChung
22-Sapphire I
(To:EK_10370207)

What you can try is to set the output result to 'String' without doing any stringify or parse to see what the service receives back from the call.

EK_10370207
5-Regular Member
(To:PaiChung)

Setting the output to String (or Text) returns the same header content as when it is JSON.

 

EK_10370207_0-1687791860058.png

 

PaiChung
22-Sapphire I
(To:EK_10370207)

Interesting, successful call shouldn't be returning those headers, those look like your call vs. a response.

I'm not sure if the Thingworx coding structure properly uses the : any that you have in there.

Maybe leave that out and directly use

result = Contentloaderfunctions .... 

 

EK_10370207
5-Regular Member
(To:PaiChung)

The ":any" is not actually in the code; it is an extension to ThingWorx that adds it to the editor.  

PaiChung
22-Sapphire I
(To:EK_10370207)

Is that response at all a 'default' response when the call doesn't resolve into a value?

ie the api has some error handling that just returns the call payload if something is correct and it can't return the value?

EK_10370207
5-Regular Member
(To:PaiChung)

No, the default response, if you ask for something that doesn't exist, is "

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable." (screenshot of Postman where I removed a char from GUID to make it not exist)

 

EK_10370207_1-1687793750793.png

 

Valid:

EK_10370207_2-1687793785787.png

 

wcui
14-Alexandrite
(To:EK_10370207)

wcui_0-1687833558008.png

TW RestAPI is not strong as Postman, the best solution is let 3rd party service send back a normal JSON format response.

Except that, hHave you tried tunning the header option of GetJSON parameter? (red line). For example, set "*/*" for Accept option of header.

Besides, there are other services you can verify(green line), such as LoadBinary, LoadJSON.

 

 

 

nmutter
14-Alexandrite
(To:EK_10370207)

I would suspect that just "somevalue" is not a valid json response. E.g. when trying to parse just a string in JavaScript

nmutter_0-1687986371815.png

Actually wikipedia states that RFC 4627 is the latest json spec since 2006. And also e.g. https://jsonformatter.curiousconcept.com/# when choosing this RFC says it is not valid

nmutter_1-1687986544548.png

 

I did not research what RFC is used by TWXs json interpreter but I assume it will be the same like I have in my browser (1st screeshot) which says its not valid.

 

Because of that I would try to just user GetText

(return type String)

result = Resources["ContenLoa..."].GetText(..

If this would have your expected response, you could interprete it like you want. Hope this works as it may complain about the actual Content-Type send by the server.. Worth a try.

I am not clear what "GetText does not work as the response is automatically formatted as JSON" means exactly - if it throws an error or what does not work here.

Top Tags