Page 1 of 1

"data" as JSON key

Posted: Sat Apr 02, 2022 11:07 pm
by infratec
Ok, not really a coding question.
But maybe it is doable with a coding trick.

I need to use ExtractJSONStructure() with a JSON key named "data".

It works, but it is very annoying and tricky:
The IDE syntax stuff always do a 'capitalization' from data.s to Data.s
then ExtractJSONStructure() fails.
The only way to run this code and compile it, is to change the D to a d and immediately run or compile it without moving the cursor
to an other line.

Is there any way to avoid this :?:

Example:

Code: Select all

Structure Example_Structure
  id.i
  data.s
  test.s
EndStructure

JSON$ = ~"{\"id\":1,\"data\":\"Hello world\",\"test\":\"PB rulez\"}"
Debug JSON$

Define Example.Example_Structure

If ParseJSON(0, JSON$)
  ExtractJSONStructure(JSONValue(0), @Example, Example_Structure)
  Debug ""
  Debug Example\id
  Debug Example\data
  Debug Example\test
EndIf
And no, I can not chnage the key of this JSON. It comes from a REST-API of a foreign server. :cry:

Maybe I should make a feature request, that when a special character is in front of a keyword, that the IDE and compiler does not use it as keyword.
Like:

Code: Select all

Structure Example_Structure
  id.i
  §data.s
  test.s
EndStructure
or

Code: Select all

Structure Example_Structure
  id.i
  'data'.s
  test.s
EndStructure

Re: "data" as JSON key

Posted: Sat Apr 02, 2022 11:38 pm
by breeze4me
Turn off the "Enable Case correction" option in the IDE preferences. :lol:
Or try the following macro.

Code: Select all

Macro data_Macro_Field_last_a
  a
EndMacro

Macro data_Macro_Field
  dat#data_Macro_Field_last_a
EndMacro


Structure Example_Structure
  id.i
  data_Macro_Field.s
  test.s
EndStructure

JSON$ = ~"{\"id\":1,\"data\":\"Hello world\",\"test\":\"PB rulez\"}"
Debug JSON$

Define Example.Example_Structure

If ParseJSON(0, JSON$)
  ExtractJSONStructure(JSONValue(0), @Example, Example_Structure)
  Debug ""
  Debug Example\id
  Debug Example\data_Macro_Field   ;AutoComplete supported
  Debug Example\test
EndIf

Or

Code: Select all

Structure Example_Structure
  id.i
  Data.s
  test.s
EndStructure

JSON$ = ~"{\"id\":1,\"data\":\"Hello world\",\"test\":\"PB rulez\"}"
Debug JSON$

Define Example.Example_Structure

If ParseJSON(0, JSON$, #PB_JSON_NoCase)
  ExtractJSONStructure(JSONValue(0), @Example, Example_Structure)
  Debug ""
  Debug Example\id
  Debug Example\Data
  Debug Example\test
EndIf

Re: "data" as JSON key

Posted: Sun Apr 03, 2022 9:21 am
by infratec
Super :!:

I think the #PB_JSON_NoCase will be the best solution (in my case),
since I don't want to disable features of the IDE.

The Macro is also a good trick.

I don't work enough with JSON or XML in PB, maybe cause I don't like them :wink: