News:

  • November 23, 2026, 10:22:08 AM

Login with username, password and session length

Author Topic: JSONBUILD and REST API  (Read 21388 times)

Bolt

  • Hero Member
  • *****
  • Posts: 598
JSONBUILD and REST API
« on: January 30, 2023, 12:49:17 PM »
I have built a JSON object into a string using JSONBUILD. Now I'm trying to access it via REST API, and It returns it as a string, which is enclosed in quotes, rendering the output an invalid JSON Object, as it returns a JSON object as a string within in an object. Are there any work around for this? I don't think it's feasible to pack all the info into a UDT, as most of the values themselves are strings, so it would be somewhat kludgy to build it all as such.

Greg

  • HostTech
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 702
  • Hmmm...
    • Host Engineering, Inc.
Re: JSONBUILD and REST API
« Reply #1 on: January 30, 2023, 04:27:17 PM »
Can you give a specific example? JSON format is an ASCII string. The only handle you have to pull, if I understand you, is the "Enclose in quotes" checkbox in the JSONBUILD "Edit Step Object Field Record" dialog.
There are two types of people in the world; those that can extrapolate from incomplete data sets.

franji1

  • Bit Weenie
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 3857
    • Host Engineering
Re: JSONBUILD and REST API
« Reply #2 on: January 30, 2023, 05:02:17 PM »
Can you give a specific example? JSON format is an ASCII string. The only handle you have to pull, if I understand you, is the "Enclose in quotes" checkbox in the JSONBUILD "Edit Step Object Field Record" dialog.

I think all strings REST API responses are enclosed in double quotes, e.g. SS0 is contains the text ABC, the RESTAPI response is 5 characters long "ABC", not just the simple 3 characters ABC.  It just so happens that SS0 contains JSON text.

Not sure if JavaScript can pre-process the RESTAPI response to remove the first and last characters if they're both double quotes???

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: JSONBUILD and REST API
« Reply #3 on: January 30, 2023, 05:03:35 PM »
Here's a simple example.
The request /data/json?SL0=SL0 returns:
Quote
SyntaxError: JSON.parse: expected ',' or '}' after property value in object at line 2 column 10 of the JSON data
Code: [Select]
{
"SL0":"{"Input10":false,"Input20":true,"Output30":false,"Name":"Smith","Text":"Sample"}"
}
Of course a request this simple could be reconstructed with ?Input10=X10&Input20=X20&Output30=Y30&Name=SS1, but you can't do the &Text="Sample" portion, and the booleans are returned as numeric.
Code: [Select]
{
"Input10":0,
"Input20":1,
"Output30":0,
"Name":"Smith"
}

Greg

  • HostTech
  • Host Moderator
  • Hero Member
  • *****
  • Posts: 702
  • Hmmm...
    • Host Engineering, Inc.
Re: JSONBUILD and REST API
« Reply #4 on: January 31, 2023, 09:35:23 AM »
When I duplicate your JSONBUILD instruction, it works. I use Do-more Designer's JSON Pretty Print display, and it doesn't complain. Also, when I do the REST API function from my Internet browser, it gives results with no complaining either. And then if I select the response and copy/paste it to the online JSON Pretty Print website, it also does not complain.

Where is the syntax error coming from?
There are two types of people in the world; those that can extrapolate from incomplete data sets.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: JSONBUILD and REST API
« Reply #5 on: January 31, 2023, 09:58:46 AM »
When I duplicate your JSONBUILD instruction, it works. I use Do-more Designer's JSON Pretty Print display, and it doesn't complain. Also, when I do the REST API function from my Internet browser, it gives results with no complaining either. And then if I select the response and copy/paste it to the online JSON Pretty Print website, it also does not complain.

Where is the syntax error coming from?

The problem is that the valid object contained within the string is being emitted as a quoted string from the REST request, but he needs the quotes stripped.

I'm looking into adding query modifier that would emit the string raw, without quotes.
« Last Edit: January 31, 2023, 10:33:21 AM by BobO »
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: JSONBUILD and REST API
« Reply #6 on: January 31, 2023, 11:18:49 AM »
OK, I've added a query modifier that emits the string raw, meaning, it assumes the string contains a well formed JSON object. This eliminates the leading and trailing quotes, and also the control characters for nested quotes. As long as everything contained in the string is valid JSON, this should work as expected.

The modifier is 'R' or 'r'. So "/data/json?fieldname=symbol,r" would return the raw value of string "symbol".

"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: JSONBUILD and REST API
« Reply #7 on: January 31, 2023, 12:25:48 PM »
Awesome, that would be great. I can work around it for now, but I'm sure it will be useful again in the future. Thanks for the speed resolution.

Any chance you could make it work to read multiple structures similar to how it can do multiple elements? /data/json?FieldName=MyStructure0,4

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: JSONBUILD and REST API
« Reply #8 on: January 31, 2023, 12:57:32 PM »
Awesome, that would be great. I can work around it for now, but I'm sure it will be useful again in the future. Thanks for the speed resolution.

Any chance you could make it work to read multiple structures similar to how it can do multiple elements? /data/json?FieldName=MyStructure0,4

I assume you have JSON records in an array of strings, and you are expecting an array of objects from the query?
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: JSONBUILD and REST API
« Reply #9 on: January 31, 2023, 01:10:12 PM »
Yes, while not specifically JSON records in an array of strings, I would expect an array of objects. I have UDT's with short single word name or status Strings, and Bits and Words for the rest of the info, trying to keep from having to query (and further reference) them individually, /data/json?FieldName0=MyStructure0&FieldName1=MyStructure1&FieldName2=MyStructure2&FieldName3=MyStructure3 for example. If they were all in an array it would make the processing code simpler/scalable.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: JSONBUILD and REST API
« Reply #10 on: January 31, 2023, 01:26:39 PM »
Done. Syntax is ",r#", where the modifier must come before the count. Was just a simple change to the parse to allow both a count and modifier. Previously they were mutually exclusive.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: JSONBUILD and REST API
« Reply #11 on: January 31, 2023, 01:42:27 PM »
Excellent. Example #16325 proving Host Engineering's support blows the rest out of the water.

Bolt

  • Hero Member
  • *****
  • Posts: 598
Re: JSONBUILD and REST API
« Reply #12 on: February 19, 2024, 03:42:32 PM »
Done. Syntax is ",r#", where the modifier must come before the count. Was just a simple change to the parse to allow both a count and modifier. Previously they were mutually exclusive.

Any idea on when this updated feature can be expected? I have an application where we're polling 16 structures the "hard" way, my developer says it's slowing things down unnecessarily.

BobO

  • Host Moderator
  • Hero Member
  • *****
  • Posts: 6180
  • Yes Pinky, Do-more will control the world!
Re: JSONBUILD and REST API
« Reply #13 on: February 19, 2024, 05:51:55 PM »
Done. Syntax is ",r#", where the modifier must come before the count. Was just a simple change to the parse to allow both a count and modifier. Previously they were mutually exclusive.

Any idea on when this updated feature can be expected? I have an application where we're polling 16 structures the "hard" way, my developer says it's slowing things down unnecessarily.

As soon as we stop being so lazy and release the firmware.  ::)

It (2.9.9) was finished, and then we tweaked a couple of things...adding network diagnostics for some of these weird situations that have happened a few times....and just haven't gotten back to releasing it. I'll poke the bear.
"It has recently come to our attention that users spend 95% of their time using 5% of the available features. That might be relevant." -BobO