"data" as JSON key

Just starting out? Need help? Post your questions and find answers here.
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

"data" as JSON key

Post 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
breeze4me
Enthusiast
Enthusiast
Posts: 633
Joined: Thu Mar 09, 2006 9:24 am
Location: S. Kor

Re: "data" as JSON key

Post 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
infratec
Always Here
Always Here
Posts: 7618
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: "data" as JSON key

Post 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:
Post Reply