[probably trivial] Access JSON objects members

Just starting out? Need help? Post your questions and find answers here.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: [probably trivial] Access JSON objects members

Post by kenmo »

vwidmer wrote:how to implement code to handle something like this:

Code: Select all

{"positions":[[123,456,"TEST1"],[234,212,"TEST2"],[982,273,"TEST3",]],"time_on_server":null}
If you try my JSON_Helper.pbi:

Code: Select all

IncludeFile "JSON_Helper.pbi"

JSON$ = ReplaceString( "{'positions':[[123,456,'TEST1'],[234,212,'TEST2'],[982,273,'TEST3']],'time_on_server':null}" , "'", #DQUOTE$)
ParseJSON(0, JSON$ )

*Pos = JSONArrayFromPath( MainJSONObject(0), "positions")
*Entry = FirstJSONChild(*Pos)
While (*Entry)
  Debug JSONIntegerFromPath(*Entry, "[0]")
  Debug JSONIntegerFromPath(*Entry, "[1]")
  Debug JSONStringFromPath(*Entry, "[2]") + #LF$
  *Entry = NextJSONChild(*Pos, *Entry)
Wend
Prints:
123
456
TEST1

234
212
TEST2

982
273
TEST3
User avatar
the.weavster
Addict
Addict
Posts: 1537
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: [probably trivial] Access JSON objects members

Post by the.weavster »

vwidmer wrote:How do u get something deeper?

like wind > VWINDBAT - SPEED2

or even deeper?

Code: Select all

{"wind":{"SPEED":"14","TEMP":"27","ANGLE":"66","VWINDBAT":{"ANGLE2":"66","SPEED2":"14","TEMP2":"27"}}}
Thanks
Hi vwidmer,

How about this:

Code: Select all

JSON$ = ~"{\"wind\":{\"SPEED\":\"14\",\"TEMP\":\"27\",\"ANGLE\":\"66\",\"VWINDBAT\":{\"ANGLE2\":\"66\",\"SPEED2\":\"14\",\"TEMP2\":\"27\"}}}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))
S$ = GetJSONString(GetJSONMember(GetJSONMember(GetJSONMember(nJS,"wind"),"VWINDBAT"),"SPEED2"))
Debug s$
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Re: [probably trivial] Access JSON objects members

Post by vwidmer »

@kenmo: Nice can you show me a couple more examples how to use your library with the previous json data. Also if I change the data to this it doesnt pull it all:

Code: Select all

{"positions":[[123.2,456.55,"TEST1"],[-234.2,212,"TEST2"],[982,-273.251,"TEST3",]],"time_on_server":null}
@the.weavster: that is much better then my code as it is much much shorter.

Thank you both very much these are GREAT!
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: [probably trivial] Access JSON objects members

Post by kenmo »

vwidmer wrote:Also if I change the data to this it doesnt pull it all:
You have a trailing comma at the end of your array, which is not considered valid JSON syntax. I had to remove that for my example.

You might be able to fix those like this:

Code: Select all

ReplaceString(JSONText$, ",]", "]")
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Re: [probably trivial] Access JSON objects members

Post by vwidmer »

Sorry took it out of a larger file.
Try this :

Code: Select all

{"positions":[[123.2,456.55,"TEST1"],[-234.2,212,"TEST2"],[982,-273.251,"TEST3"]],"time_on_server":null}
What I ment is it will pull only the 123 not 123.2
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: [probably trivial] Access JSON objects members

Post by kenmo »

I just updated the includefile, download it again.

I added JSONFloatFromPath() and JSONDoubleFromPath(), you can use those.
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Re: [probably trivial] Access JSON objects members

Post by vwidmer »

@kenmo: you rock! Thanks works very very nice.

Thanks again every one.
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
User avatar
the.weavster
Addict
Addict
Posts: 1537
Joined: Thu Jul 03, 2003 6:53 pm
Location: England

Re: [probably trivial] Access JSON objects members

Post by the.weavster »

vwidmer wrote:@the.weavster: that is much better then my code as it is much much shorter.
No problem.

If you don't want too many levels of nested commands you could do something like this:

Code: Select all

Procedure.i GetJSONChild(nj,pth.s)
  nReps = CountString(pth,"|")+1
  js = nj
  For nLoop = 1 To nReps
    js = GetJSONMember(js,StringField(pth,nLoop,"|"))
    If Not js : ProcedureReturn -1 : EndIf
  Next
  ProcedureReturn js
EndProcedure

JSON$ = ~"{\"wind\":{\"SPEED\":\"14\",\"TEMP\":\"27\",\"ANGLE\":\"66\",\"VWINDBAT\":{\"ANGLE2\":\"66\",\"SPEED2\":\"14\",\"TEMP2\":\"27\"}}}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))

nJC = GetJSONChild(nJS,"wind|VWINDBAT|SPEED2")
If nJC = -1
  Debug "Invalid path"
Else
  Debug GetJSONString(nJC)
EndIf


; or for your second example
JSON$ = ~"{\"positions\":[[123.2,456.55,\"TEST1\"],[-234.2,212,\"TEST2\"],[982,-273.251,\"TEST3\"]],\"time_on_server\":null}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))

nJC = GetJSONChild(nJS,"positions")
Debug GetJSONDouble(GetJSONElement(GetJSONElement(nJC,0),0))
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Re: [probably trivial] Access JSON objects members

Post by vwidmer »

@the.weavster: Wow I like this code as well very nice example. Thank you
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Re: [probably trivial] Access JSON objects members

Post by vwidmer »

How to loop the second example to get all?
the.weavster wrote:
vwidmer wrote:@the.weavster: that is much better then my code as it is much much shorter.
No problem.

If you don't want too many levels of nested commands you could do something like this:

Code: Select all

Procedure.i GetJSONChild(nj,pth.s)
  nReps = CountString(pth,"|")+1
  js = nj
  For nLoop = 1 To nReps
    js = GetJSONMember(js,StringField(pth,nLoop,"|"))
    If Not js : ProcedureReturn -1 : EndIf
  Next
  ProcedureReturn js
EndProcedure

JSON$ = ~"{\"wind\":{\"SPEED\":\"14\",\"TEMP\":\"27\",\"ANGLE\":\"66\",\"VWINDBAT\":{\"ANGLE2\":\"66\",\"SPEED2\":\"14\",\"TEMP2\":\"27\"}}}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))

nJC = GetJSONChild(nJS,"wind|VWINDBAT|SPEED2")
If nJC = -1
  Debug "Invalid path"
Else
  Debug GetJSONString(nJC)
EndIf


; or for your second example
JSON$ = ~"{\"positions\":[[123.2,456.55,\"TEST1\"],[-234.2,212,\"TEST2\"],[982,-273.251,\"TEST3\"]],\"time_on_server\":null}"
nJS = JSONValue(ParseJSON(#PB_Any,JSON$))

nJC = GetJSONChild(nJS,"positions")
Debug GetJSONDouble(GetJSONElement(GetJSONElement(nJC,0),0))
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
vwidmer
Enthusiast
Enthusiast
Posts: 282
Joined: Mon Jan 20, 2014 6:32 pm

Re: [probably trivial] Access JSON objects members

Post by vwidmer »

How would I grab a child in a child using your mod?

Code: Select all

"Data":[[1,"test 1","test 2",{"min":5,"max":10},false,null]
Thanks
kenmo wrote:I just updated the includefile, download it again.

I added JSONFloatFromPath() and JSONDoubleFromPath(), you can use those.
WARNING: I dont know what I am doing! I just put stuff here and there and sometimes like magic it works. So please improve on my code and post your changes so I can learn more. TIA
Post Reply