Sample code for 30+ languages & platforms
PowerBuilder

Loading and Parsing a Complex JSON Array

See more JSON Examples

This example loads a JSON array containing more complex data. It shows how to parse (access) various values contained within the JSON.

Chilkat PowerBuilder Downloads

PowerBuilder
integer li_rc
integer li_Success
oleobject loo_Sb
integer li_BCrlf
oleobject loo_JsonArray
integer li_NumRecords
integer i
oleobject loo_JsonRecord
integer li_NumTelefones
integer j
integer li_NumContatos
integer k
integer li_NumEmails

li_Success = 0

// This is the JSON we'll be loading:

// 	[
// 	  {
// 	    "telefones": [
// 	      {
// 	        "numero": "19995555555",
// 	        "tipo": "T",
// 	        "id": 2541437
// 	      } 
// 	    ],
// 	    "cnpj": "11395551000164",
// 	    "rua": "R XAVIER AUGUSTO ROGGE, 22",
// 	    "complemento": "",
// 	    "contatos": [
// 	    ],
// 	    "tipo": "J",
// 	    "razao_social": "SOUP BRASIL LTDA - ME",
// 	    "nome_fantasia": "SOUP BRASIL",
// 	    "bairro": "ABC DOS COLIBRIS",
// 	    "cidade": "TEST",
// 	    "inscricao_estadual": "222.102.222.116",
// 	    "observacao": "",
// 	    "id": 2209595,
// 	    "ultima_alteracao": "2016-12-26 16:22:34",
// 	    "cep": "13555000",
// 	    "suframa": "",
// 	    "estado": "SP",
// 	    "emails": [
// 	      {
// 	        "email": "somebody@terra.com.br",
// 	        "tipo": "T",
// 	        "id": 1065557
// 	      } 
// 	    ],
// 	    "excluido": false
// 	  },
// 	  {
// 	    "telefones": [
// 	    ],
// 	    "cnpj": "12496555500180",
// 	    "rua": "AV ROLF WIEST, 100",
// 	    "complemento": "ANDAR 7 SALA 612 A 620",
// 	    "contatos": [
// 	    ],
// 	    "tipo": "J",
// 	    "razao_social": "SIMPLE SOFTWARE LTDA",
// 	    "nome_fantasia": "",
// 	    "bairro": "DOM ZETIRO",
// 	    "cidade": "APARTVILLE",
// 	    "inscricao_estadual": "",
// 	    "observacao": "",
// 	    "id": 2255594,
// 	    "ultima_alteracao": "2016-12-26 16:28:31",
// 	    "cep": "89255505",
// 	    "suframa": "",
// 	    "estado": "SC",
// 	    "emails": [
// 	    ],
// 	    "excluido": false
// 	  },
// 	  {
// 	    "telefones": [
// 	      {
// 	        "numero": "1938655556",
// 	        "tipo": "T",
// 	        "id": 2555438
// 	      } 
// 	    ],
// 	    "cnpj": "00003555500153",
// 	    "rua": "AV ABCDEF PINTO CATAO, 18",
// 	    "complemento": "",
// 	    "contatos": [
// 	      {
// 	        "telefones": [
// 	          {
// 	            "numero": "1999655554",
// 	            "tipo": "T",
// 	            "id": 2555559
// 	          } 
// 	        ],
// 	        "cargo": "zzz de compras",
// 	        "nome": "Gerard",
// 	        "emails": [
// 	          {
// 	            "email": "gerard@terra.com.br",
// 	            "tipo": "T",
// 	            "id": 1065559
// 	          } 
// 	        ],
// 	        "id": 844485,
// 	        "excluido": false
// 	      } 
// 	    ],
// 	    "tipo": "J",
// 	    "razao_social": "TIDY TECNOLOGIA LTDA - EPP",
// 	    "nome_fantasia": "TIDY",
// 	    "bairro": "TUNA",
// 	    "cidade": "JAGUAR",
// 	    "inscricao_estadual": "395.222.441.222",
// 	    "observacao": "ligar sempre depois das 14hs",
// 	    "id": 2255597,
// 	    "ultima_alteracao": "2016-12-28 07:31:52",
// 	    "cep": "13555500",
// 	    "suframa": "",
// 	    "estado": "SP",
// 	    "emails": [
// 	      {
// 	        "email": "xi@tidy.com.br",
// 	        "tipo": "T",
// 	        "id": 10655558
// 	      } 
// 	    ],
// 	    "excluido": false
// 	  } 
// 	]
// 

// Construct a StringBuilder containing the above JSON array.
loo_Sb = create oleobject
li_rc = loo_Sb.ConnectToNewObject("Chilkat.StringBuilder")
if li_rc < 0 then
    destroy loo_Sb
    MessageBox("Error","Connecting to COM object failed")
    return
end if
li_BCrlf = 1
loo_Sb.AppendLine("[",li_BCrlf)
loo_Sb.AppendLine("  {",li_BCrlf)
loo_Sb.AppendLine("    ~"telefones~": [",li_BCrlf)
loo_Sb.AppendLine("      {",li_BCrlf)
loo_Sb.AppendLine("        ~"numero~": ~"19995555555~",",li_BCrlf)
loo_Sb.AppendLine("        ~"tipo~": ~"T~",",li_BCrlf)
loo_Sb.AppendLine("        ~"id~": 2541437",li_BCrlf)
loo_Sb.AppendLine("      }",li_BCrlf)
loo_Sb.AppendLine("    ],",li_BCrlf)
loo_Sb.AppendLine("    ~"cnpj~": ~"11395551000164~",",li_BCrlf)
loo_Sb.AppendLine("    ~"rua~": ~"R XAVIER AUGUSTO ROGGE, 22~",",li_BCrlf)
loo_Sb.AppendLine("    ~"complemento~": ~"~",",li_BCrlf)
loo_Sb.AppendLine("    ~"contatos~": [",li_BCrlf)
loo_Sb.AppendLine("    ],",li_BCrlf)
loo_Sb.AppendLine("    ~"tipo~": ~"J~",",li_BCrlf)
loo_Sb.AppendLine("    ~"razao_social~": ~"SOUP BRASIL LTDA - ME~",",li_BCrlf)
loo_Sb.AppendLine("    ~"nome_fantasia~": ~"SOUP BRASIL~",",li_BCrlf)
loo_Sb.AppendLine("    ~"bairro~": ~"ABC DOS COLIBRIS~",",li_BCrlf)
loo_Sb.AppendLine("    ~"cidade~": ~"TEST~",",li_BCrlf)
loo_Sb.AppendLine("    ~"inscricao_estadual~": ~"222.102.222.116~",",li_BCrlf)
loo_Sb.AppendLine("    ~"observacao~": ~"~",",li_BCrlf)
loo_Sb.AppendLine("    ~"id~": 2209595,",li_BCrlf)
loo_Sb.AppendLine("    ~"ultima_alteracao~": ~"2016-12-26 16:22:34~",",li_BCrlf)
loo_Sb.AppendLine("    ~"cep~": ~"13555000~",",li_BCrlf)
loo_Sb.AppendLine("    ~"suframa~": ~"~",",li_BCrlf)
loo_Sb.AppendLine("    ~"estado~": ~"SP~",",li_BCrlf)
loo_Sb.AppendLine("    ~"emails~": [",li_BCrlf)
loo_Sb.AppendLine("      {",li_BCrlf)
loo_Sb.AppendLine("        ~"email~": ~"somebody@terra.com.br~",",li_BCrlf)
loo_Sb.AppendLine("        ~"tipo~": ~"T~",",li_BCrlf)
loo_Sb.AppendLine("        ~"id~": 1065557",li_BCrlf)
loo_Sb.AppendLine("      }",li_BCrlf)
loo_Sb.AppendLine("    ],",li_BCrlf)
loo_Sb.AppendLine("    ~"excluido~": false",li_BCrlf)
loo_Sb.AppendLine("  },",li_BCrlf)
loo_Sb.AppendLine("  {",li_BCrlf)
loo_Sb.AppendLine("    ~"telefones~": [",li_BCrlf)
loo_Sb.AppendLine("    ],",li_BCrlf)
loo_Sb.AppendLine("    ~"cnpj~": ~"12496555500180~",",li_BCrlf)
loo_Sb.AppendLine("    ~"rua~": ~"AV ROLF WIEST, 100~",",li_BCrlf)
loo_Sb.AppendLine("    ~"complemento~": ~"ANDAR 7 SALA 612 A 620~",",li_BCrlf)
loo_Sb.AppendLine("    ~"contatos~": [",li_BCrlf)
loo_Sb.AppendLine("    ],",li_BCrlf)
loo_Sb.AppendLine("    ~"tipo~": ~"J~",",li_BCrlf)
loo_Sb.AppendLine("    ~"razao_social~": ~"SIMPLE SOFTWARE LTDA~",",li_BCrlf)
loo_Sb.AppendLine("    ~"nome_fantasia~": ~"~",",li_BCrlf)
loo_Sb.AppendLine("    ~"bairro~": ~"DOM ZETIRO~",",li_BCrlf)
loo_Sb.AppendLine("    ~"cidade~": ~"APARTVILLE~",",li_BCrlf)
loo_Sb.AppendLine("    ~"inscricao_estadual~": ~"~",",li_BCrlf)
loo_Sb.AppendLine("    ~"observacao~": ~"~",",li_BCrlf)
loo_Sb.AppendLine("    ~"id~": 2255594,",li_BCrlf)
loo_Sb.AppendLine("    ~"ultima_alteracao~": ~"2016-12-26 16:28:31~",",li_BCrlf)
loo_Sb.AppendLine("    ~"cep~": ~"89255505~",",li_BCrlf)
loo_Sb.AppendLine("    ~"suframa~": ~"~",",li_BCrlf)
loo_Sb.AppendLine("    ~"estado~": ~"SC~",",li_BCrlf)
loo_Sb.AppendLine("    ~"emails~": [",li_BCrlf)
loo_Sb.AppendLine("    ],",li_BCrlf)
loo_Sb.AppendLine("    ~"excluido~": false",li_BCrlf)
loo_Sb.AppendLine("  },",li_BCrlf)
loo_Sb.AppendLine("  {",li_BCrlf)
loo_Sb.AppendLine("    ~"telefones~": [",li_BCrlf)
loo_Sb.AppendLine("      {",li_BCrlf)
loo_Sb.AppendLine("        ~"numero~": ~"1938655556~",",li_BCrlf)
loo_Sb.AppendLine("        ~"tipo~": ~"T~",",li_BCrlf)
loo_Sb.AppendLine("        ~"id~": 2555438",li_BCrlf)
loo_Sb.AppendLine("      }",li_BCrlf)
loo_Sb.AppendLine("    ],",li_BCrlf)
loo_Sb.AppendLine("    ~"cnpj~": ~"00003555500153~",",li_BCrlf)
loo_Sb.AppendLine("    ~"rua~": ~"AV ABCDEF PINTO CATAO, 18~",",li_BCrlf)
loo_Sb.AppendLine("    ~"complemento~": ~"~",",li_BCrlf)
loo_Sb.AppendLine("    ~"contatos~": [",li_BCrlf)
loo_Sb.AppendLine("      {",li_BCrlf)
loo_Sb.AppendLine("        ~"telefones~": [",li_BCrlf)
loo_Sb.AppendLine("          {",li_BCrlf)
loo_Sb.AppendLine("            ~"numero~": ~"1999655554~",",li_BCrlf)
loo_Sb.AppendLine("            ~"tipo~": ~"T~",",li_BCrlf)
loo_Sb.AppendLine("            ~"id~": 2555559",li_BCrlf)
loo_Sb.AppendLine("          }",li_BCrlf)
loo_Sb.AppendLine("        ],",li_BCrlf)
loo_Sb.AppendLine("        ~"cargo~": ~"zzz de compras~",",li_BCrlf)
loo_Sb.AppendLine("        ~"nome~": ~"Gerard~",",li_BCrlf)
loo_Sb.AppendLine("        ~"emails~": [",li_BCrlf)
loo_Sb.AppendLine("          {",li_BCrlf)
loo_Sb.AppendLine("            ~"email~": ~"gerard@terra.com.br~",",li_BCrlf)
loo_Sb.AppendLine("            ~"tipo~": ~"T~",",li_BCrlf)
loo_Sb.AppendLine("            ~"id~": 1065559",li_BCrlf)
loo_Sb.AppendLine("          }",li_BCrlf)
loo_Sb.AppendLine("        ],",li_BCrlf)
loo_Sb.AppendLine("        ~"id~": 844485,",li_BCrlf)
loo_Sb.AppendLine("        ~"excluido~": false",li_BCrlf)
loo_Sb.AppendLine("      }",li_BCrlf)
loo_Sb.AppendLine("    ],",li_BCrlf)
loo_Sb.AppendLine("    ~"tipo~": ~"J~",",li_BCrlf)
loo_Sb.AppendLine("    ~"razao_social~": ~"TIDY TECNOLOGIA LTDA - EPP~",",li_BCrlf)
loo_Sb.AppendLine("    ~"nome_fantasia~": ~"TIDY~",",li_BCrlf)
loo_Sb.AppendLine("    ~"bairro~": ~"TUNA~",",li_BCrlf)
loo_Sb.AppendLine("    ~"cidade~": ~"JAGUAR~",",li_BCrlf)
loo_Sb.AppendLine("    ~"inscricao_estadual~": ~"395.222.441.222~",",li_BCrlf)
loo_Sb.AppendLine("    ~"observacao~": ~"ligar sempre depois das 14hs~",",li_BCrlf)
loo_Sb.AppendLine("    ~"id~": 2255597,",li_BCrlf)
loo_Sb.AppendLine("    ~"ultima_alteracao~": ~"2016-12-28 07:31:52~",",li_BCrlf)
loo_Sb.AppendLine("    ~"cep~": ~"13555500~",",li_BCrlf)
loo_Sb.AppendLine("    ~"suframa~": ~"~",",li_BCrlf)
loo_Sb.AppendLine("    ~"estado~": ~"SP~",",li_BCrlf)
loo_Sb.AppendLine("    ~"emails~": [",li_BCrlf)
loo_Sb.AppendLine("      {",li_BCrlf)
loo_Sb.AppendLine("        ~"email~": ~"xi@tidy.com.br~",",li_BCrlf)
loo_Sb.AppendLine("        ~"tipo~": ~"T~",",li_BCrlf)
loo_Sb.AppendLine("        ~"id~": 10655558",li_BCrlf)
loo_Sb.AppendLine("      }",li_BCrlf)
loo_Sb.AppendLine("    ],",li_BCrlf)
loo_Sb.AppendLine("    ~"excluido~": false",li_BCrlf)
loo_Sb.AppendLine("  }",li_BCrlf)
loo_Sb.AppendLine("]",li_BCrlf)

// Load the JSON array into a JsonArray:
loo_JsonArray = create oleobject
li_rc = loo_JsonArray.ConnectToNewObject("Chilkat.JsonArray")

li_Success = loo_JsonArray.LoadSb(loo_Sb)
if li_Success <> 1 then
    Write-Debug loo_JsonArray.LastErrorText
    destroy loo_Sb
    destroy loo_JsonArray
    return
end if

// Get some information from each record in the array.
li_NumRecords = loo_JsonArray.Size
i = 0
do while i < li_NumRecords
    Write-Debug "------ Record " + string(i) + " -------"

    loo_JsonRecord = loo_JsonArray.ObjectAt(i)

    // Examine information for this record
    li_NumTelefones = loo_JsonRecord.SizeOfArray("telefones")
    Write-Debug "Number of telefones: " + string(li_NumTelefones)
    j = 0
    do while j < li_NumTelefones
        loo_JsonRecord.J = j
        Write-Debug "  telefones numero: " + loo_JsonRecord.StringOf("telefones[j].numero")
        Write-Debug "  telefones tipo: " + loo_JsonRecord.StringOf("telefones[j].tipo")
        Write-Debug "  telefones id: " + loo_JsonRecord.StringOf("telefones[j].id")
        j = j + 1
    loop

    Write-Debug "cnpj: " + loo_JsonRecord.StringOf("cnpj")
    Write-Debug "rua: " + loo_JsonRecord.StringOf("rua")
    // ...

    li_NumContatos = loo_JsonRecord.SizeOfArray("contatos")
    Write-Debug "Number of contatos: " + string(li_NumContatos)
    j = 0
    do while j < li_NumContatos
        loo_JsonRecord.J = j

        li_NumTelefones = loo_JsonRecord.SizeOfArray("contatos[j].telefones")
        Write-Debug "  Number of telefones: " + string(li_NumTelefones)
        k = 0
        do while k < li_NumTelefones
            loo_JsonRecord.K = k
            Write-Debug "  telefones numero: " + loo_JsonRecord.StringOf("contatos[j].telefones[k].numero")
            Write-Debug "  telefones tipo: " + loo_JsonRecord.StringOf("contatos[j].telefones[k].tipo")
            Write-Debug "  telefones id: " + loo_JsonRecord.StringOf("contatos[j].telefones[k].id")
            k = k + 1
        loop

        Write-Debug "  cargo: " + loo_JsonRecord.StringOf("contatos[j].cargo")

        li_NumEmails = loo_JsonRecord.SizeOfArray("contatos[j].emails")
        Write-Debug "  Number of emails: " + string(li_NumEmails)
        k = 0
        do while k < li_NumEmails
            loo_JsonRecord.K = k
            Write-Debug "  emails email: " + loo_JsonRecord.StringOf("contatos[j].emails[k].email")
            Write-Debug "  emails tipo: " + loo_JsonRecord.StringOf("contatos[j].emails[k].tipo")
            Write-Debug "  emails id: " + loo_JsonRecord.StringOf("contatos[j].emails[k].id")
            k = k + 1
        loop

        j = j + 1
    loop

    destroy loo_JsonRecord
    i = i + 1
loop

// The output for the above code is:

// 	------ Record 0 -------
// 	Number of telefones: 1
// 	  telefones numero: 19995555555
// 	  telefones tipo: T
// 	  telefones id: 2541437
// 	cnpj: 11395551000164
// 	rua: R XAVIER AUGUSTO ROGGE, 22
// 	Number of contatos: 0
// 	------ Record 1 -------
// 	Number of telefones: 0
// 	cnpj: 12496555500180
// 	rua: AV ROLF WIEST, 100
// 	Number of contatos: 0
// 	------ Record 2 -------
// 	Number of telefones: 1
// 	  telefones numero: 1938655556
// 	  telefones tipo: T
// 	  telefones id: 2555438
// 	cnpj: 00003555500153
// 	rua: AV ABCDEF PINTO CATAO, 18
// 	Number of contatos: 1
// 	  Number of telefones: 1
// 	  telefones numero: 1999655554
// 	  telefones tipo: T
// 	  telefones id: 2555559
// 	  cargo: zzz de compras
// 	  Number of emails: 1
// 	  emails email: gerard@terra.com.br
// 	  emails tipo: T
// 	  emails id: 1065559
// 


destroy loo_Sb
destroy loo_JsonArray