JSON Nested Objects

Just starting out? Need help? Post your questions and find answers here.
User avatar
Plan_B
New User
New User
Posts: 4
Joined: Thu Nov 23, 2017 5:32 pm

JSON Nested Objects

Post by Plan_B »

Hi everybody

Testing PB right now, love it! I'm parsing a JSON successfully but have trouble on understanding how to handle nested objects. I'm using a simple dummy json for testing:

Code: Select all

[
  {
    "_id": "5a16ed1b873eb495e083fd29",
    "index": 0,
    "guid": "9f54a017-50bf-47c0-a3e1-0710154b7f04",
    "isActive": true,
    "balance": "$2,953.75",
    "picture": "http://placehold.it/32x32",
    "age": 31,
    "eyeColor": "brown",
    "name": "Leonor Blevins",
    "gender": "female",
    "company": "ERSUM",
    "email": "leonorblevins@ersum.com",
    "phone": "+1 (850) 456-2539",
    "address": "905 Lenox Road, Disautel, Delaware, 4197",
    "about": "Ipsum ut magna id in. blah blah blah.\r\n",
    "registered": "2016-04-17T06:27:14 -02:00",
    "latitude": -56.411933,
    "longitude": -25.603861,
    "tags": [
      "reprehenderit",
      "quis",
      "commodo",
      "consequat",
      "enim",
      "velit",
      "tempor"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Bertha Matthews"
      },
      {
        "id": 1,
        "name": "Henry Glover"
      },
      {
        "id": 2,
        "name": "Frye Bruce"
      }
    ],
    "greeting": "Hello, Leonor Blevins! You have 2 unread messages.",
    "favoriteFruit": "banana"
  }
]
I'm parsing the JSON directly into the Structure with a List via ExtractJSONList():

Code: Select all

;# Structure
Structure Example
  _id.s
  index.l
  guid.s
  isActive.l
  balance.s
  picture.s
  age.l
  eyeColor.s
  name.s
  gender.s
  company.s
  email.s
  phone.s
  address.s
  about.s
  registered.s
  latitude.l
  longitude.l
  List tags.s()
  ;TODO friends
  greeting.s
  favoriteFruit.s
EndStructure

;# Enumeration
Enumeration
  #J ;JSON File
EndEnumeration

;# Globals
Global NewList Example.Example()

;# Load JSON
If LoadJSON(#J, "example-simple.json")
  Debug "example.json loaded"
  ExtractJSONList(JSONValue(#J), Example())
Else
  Debug "Can not load example.json"
EndIf


;Debug
Debug ""
Debug "---"
Debug ""
ForEach Example()
  Debug "_id.l: " + Example()\_id
  Debug "guid.s: " + Example()\guid
  Debug "isActive.l: " + Example()\isActive
  Debug "balance.s: " + Example()\balance
  Debug "picture.s: " + Example()\picture
  Debug "age.l: " + Example()\age
  Debug "eyeColor.s: " + Example()\eyeColor
  Debug "name.s: " + Example()\name
  Debug "gender.s: " + Example()\gender
  Debug "company.s: " + Example()\company
  Debug "email.s: " + Example()\company
  Debug "phone.s" + Example()\phone
  Debug "address.s: " + Example()\address
  Debug "about.s: " + Example()\about
  Debug "registered.s: " + Example()\registered
  Debug "latitude.l: " + Example()\latitude
  Debug "longitude.l: " + Example()\longitude
  
  ;List tags()
  ForEach Example()\tags()
    Debug Chr(9) + "tags: " + Example()\tags()
  Next
  
  ;List friends()
  ;TODO
  
  Debug "greeting.s: " + Example()\greeting
  Debug "favoriteFruit.s: " + Example()\favoriteFruit
  Debug ""
  Debug "---"
  Debug ""
Next
As you can see I have a List tags.s() which wondrously parses the strings into an array. However, how would I go about implementing the friends Object (with mixed types) into the Structure? I've tried with Structure Extends but that doesn't work since both structures use the "id" string field.

How do you guys parse JSON Objects inside another JSON Object is the question basically.

Very impressed on how quick I can prototype stuff in PB. Looking forward to learn more!

Thanks for reading :)
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: JSON Nested Objects

Post by #NULL »

hello
maybe like this:
- define a friend structure
- define a friend list field
- loop over the tag friends

Code: Select all

Structure sFriend
  id.i
  name.s
EndStructure

;# Structure
Structure Example
  _id.s
  index.l
  guid.s
  isActive.l
  balance.s
  picture.s
  age.l
  eyeColor.s
  name.s
  gender.s
  company.s
  email.s
  phone.s
  address.s
  about.s
  registered.s
  latitude.l
  longitude.l
  List tags.s()
  List friends.sFriend()
  greeting.s
  favoriteFruit.s
EndStructure

;# Enumeration
Enumeration
  #J ;JSON File
EndEnumeration

;# Globals
Global NewList Example.Example()

Debug GetCurrentDirectory()
Debug FileSize("json_tmp.json")


;# Load JSON
If LoadJSON(#J, "json_tmp.json")
  Debug "example.json loaded"
  ExtractJSONList(JSONValue(#J), Example())
Else
  Debug "Can not load example.json"
  Debug JSONErrorMessage()
  Debug JSONErrorLine()
  Debug JSONErrorPosition()
EndIf


;Debug
Debug ""
Debug "---"
Debug ""
ForEach Example()
  Debug "_id.l: " + Example()\_id
  Debug "guid.s: " + Example()\guid
  Debug "isActive.l: " + Example()\isActive
  Debug "balance.s: " + Example()\balance
  Debug "picture.s: " + Example()\picture
  Debug "age.l: " + Example()\age
  Debug "eyeColor.s: " + Example()\eyeColor
  Debug "name.s: " + Example()\name
  Debug "gender.s: " + Example()\gender
  Debug "company.s: " + Example()\company
  Debug "email.s: " + Example()\company
  Debug "phone.s" + Example()\phone
  Debug "address.s: " + Example()\address
  Debug "about.s: " + Example()\about
  Debug "registered.s: " + Example()\registered
  Debug "latitude.l: " + Example()\latitude
  Debug "longitude.l: " + Example()\longitude
 
  ;List tags()
  ForEach Example()\tags()
    Debug Chr(9) + "tags: " + Example()\tags()
  Next
 
  ;List friends()
  ForEach Example()\friends()
    Debug ""
    Debug Chr(9) + "friends: " + Example()\friends()\id
    Debug Chr(9) + "friends: " + Example()\friends()\name
  Next
 
  Debug "greeting.s: " + Example()\greeting
  Debug "favoriteFruit.s: " + Example()\favoriteFruit
  Debug ""
  Debug "---"
  Debug ""
Next
User avatar
Plan_B
New User
New User
Posts: 4
Joined: Thu Nov 23, 2017 5:32 pm

Re: JSON Nested Objects

Post by Plan_B »

Oh wow, it is that simple. Exactly what I'm looking for :)
Thanks #NULL!
Post Reply