Look for key in JSON

Just starting out? Need help? Post your questions and find answers here.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Look for key in JSON

Post by collectordave »

I am attempting to look for a particular key in a JSON by using a repeat loop as below:-

Code: Select all

Repeat
  
  If JSONType(json) = #PB_JSON_Object
      
    If ExamineJSONMembers(json)
      
      While NextJSONMember(json)
        
        If JSONMemberKey(json) = "Key to look for"
          Quit = #True 
        EndIf      
        
        NextJson = JSONMemberValue(json)
          
        json = NextJson

      Wend
        
    EndIf
    
  EndIf
              
Until Quit = #True
It allways fails on

While NextJSONMember(json)

Not a clue where I am going wrong

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
User avatar
spikey
Enthusiast
Enthusiast
Posts: 586
Joined: Wed Sep 22, 2010 1:17 pm
Location: United Kingdom

Re: Look for key in JSON

Post by spikey »

You're overriding the tree structures position and pointing it to something else with these lines:-

Code: Select all

NextJson = JSONMemberValue(json)
         
json = NextJson
At a guess JSONMember <> JSONMemberValue; there is no NextJSONMember to find when the loop iterates and the function call fails.
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: Look for key in JSON

Post by kenmo »

Yes, you start examining a JSON object, then after 1 loop you change to its first child (which might not be an Object type!) and try to CONTINUE the search on this different node. It doesn't seem written correctly.

Do you have an example of the JSON you're searching?

Are you trying to search just one level "deep" or the entire JSON tree... if the entire tree, then a recursive procedure is probably best. Easy to implement.
collectordave
Addict
Addict
Posts: 1309
Joined: Fri Aug 28, 2015 6:10 pm
Location: Portugal

Re: Look for key in JSON

Post by collectordave »

Gave up on repeat loop using recursive code here viewtopic.php?f=13&t=73443

Still one more little problem with synbols they have used @ for attributes I am attempting to extract with extractjsonarray.

Thanks all

CD
Any intelligent fool can make things bigger and more complex. It takes a touch of genius — and a lot of courage to move in the opposite direction.
Post Reply