Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
Local $bSuccess = False

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

; Load the JSON array into a JsonArray:
$oJsonArray = ObjCreate("Chilkat.JsonArray")
$bSuccess = $oJsonArray.LoadSb($oSb)
If ($bSuccess <> True) Then
    ConsoleWrite($oJsonArray.LastErrorText & @CRLF)
    Exit
EndIf

; Get some information from each record in the array.
Local $iNumRecords = $oJsonArray.Size
Local $i = 0
While $i < $iNumRecords
    ConsoleWrite("------ Record " & $i & " -------" & @CRLF)

Local $oJsonRecord = $oJsonArray.ObjectAt($i)

    ; Examine information for this record
Local $iNumTelefones = $oJsonRecord.SizeOfArray("telefones")
    ConsoleWrite("Number of telefones: " & $iNumTelefones & @CRLF)
Local $iJ = 0
    While $iJ < $iNumTelefones
        $oJsonRecord.J = $iJ
        ConsoleWrite("  telefones numero: " & $oJsonRecord.StringOf("telefones[j].numero") & @CRLF)
        ConsoleWrite("  telefones tipo: " & $oJsonRecord.StringOf("telefones[j].tipo") & @CRLF)
        ConsoleWrite("  telefones id: " & $oJsonRecord.StringOf("telefones[j].id") & @CRLF)
        $iJ = $iJ + 1
    Wend

    ConsoleWrite("cnpj: " & $oJsonRecord.StringOf("cnpj") & @CRLF)
    ConsoleWrite("rua: " & $oJsonRecord.StringOf("rua") & @CRLF)
    ; ...

Local $iNumContatos = $oJsonRecord.SizeOfArray("contatos")
    ConsoleWrite("Number of contatos: " & $iNumContatos & @CRLF)
    $iJ = 0
    While $iJ < $iNumContatos
        $oJsonRecord.J = $iJ

        $iNumTelefones = $oJsonRecord.SizeOfArray("contatos[j].telefones")
        ConsoleWrite("  Number of telefones: " & $iNumTelefones & @CRLF)
Local $iK = 0
        While $iK < $iNumTelefones
            $oJsonRecord.K = $iK
            ConsoleWrite("  telefones numero: " & $oJsonRecord.StringOf("contatos[j].telefones[k].numero") & @CRLF)
            ConsoleWrite("  telefones tipo: " & $oJsonRecord.StringOf("contatos[j].telefones[k].tipo") & @CRLF)
            ConsoleWrite("  telefones id: " & $oJsonRecord.StringOf("contatos[j].telefones[k].id") & @CRLF)
            $iK = $iK + 1
        Wend

        ConsoleWrite("  cargo: " & $oJsonRecord.StringOf("contatos[j].cargo") & @CRLF)

Local $iNumEmails = $oJsonRecord.SizeOfArray("contatos[j].emails")
        ConsoleWrite("  Number of emails: " & $iNumEmails & @CRLF)
        $iK = 0
        While $iK < $iNumEmails
            $oJsonRecord.K = $iK
            ConsoleWrite("  emails email: " & $oJsonRecord.StringOf("contatos[j].emails[k].email") & @CRLF)
            ConsoleWrite("  emails tipo: " & $oJsonRecord.StringOf("contatos[j].emails[k].tipo") & @CRLF)
            ConsoleWrite("  emails id: " & $oJsonRecord.StringOf("contatos[j].emails[k].id") & @CRLF)
            $iK = $iK + 1
        Wend

        $iJ = $iJ + 1
    Wend

    $i = $i + 1
Wend

; 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
;