Sample code for 30+ languages & platforms
Visual FoxPro

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 Visual FoxPro Downloads

Visual FoxPro
LOCAL lnSuccess
LOCAL loSb
LOCAL lnBCrlf
LOCAL loJsonArray
LOCAL lnNumRecords
LOCAL i
LOCAL loJsonRecord
LOCAL lnNumTelefones
LOCAL j
LOCAL lnNumContatos
LOCAL k
LOCAL lnNumEmails

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

* Load the JSON array into a JsonArray:
loJsonArray = CreateObject('Chilkat.JsonArray')
lnSuccess = loJsonArray.LoadSb(loSb)
IF (lnSuccess <> 1) THEN
    ? loJsonArray.LastErrorText
    RELEASE loSb
    RELEASE loJsonArray
    CANCEL
ENDIF

* Get some information from each record in the array.
lnNumRecords = loJsonArray.Size
i = 0
DO WHILE i < lnNumRecords
    ? "------ Record " + STR(i) + " -------"

    loJsonRecord = loJsonArray.ObjectAt(i)

    * Examine information for this record
    lnNumTelefones = loJsonRecord.SizeOfArray("telefones")
    ? "Number of telefones: " + STR(lnNumTelefones)
    j = 0
    DO WHILE j < lnNumTelefones
        loJsonRecord.J = j
        ? "  telefones numero: " + loJsonRecord.StringOf("telefones[j].numero")
        ? "  telefones tipo: " + loJsonRecord.StringOf("telefones[j].tipo")
        ? "  telefones id: " + loJsonRecord.StringOf("telefones[j].id")
        j = j + 1
    ENDDO

    ? "cnpj: " + loJsonRecord.StringOf("cnpj")
    ? "rua: " + loJsonRecord.StringOf("rua")
    * ...

    lnNumContatos = loJsonRecord.SizeOfArray("contatos")
    ? "Number of contatos: " + STR(lnNumContatos)
    j = 0
    DO WHILE j < lnNumContatos
        loJsonRecord.J = j

        lnNumTelefones = loJsonRecord.SizeOfArray("contatos[j].telefones")
        ? "  Number of telefones: " + STR(lnNumTelefones)
        k = 0
        DO WHILE k < lnNumTelefones
            loJsonRecord.K = k
            ? "  telefones numero: " + loJsonRecord.StringOf("contatos[j].telefones[k].numero")
            ? "  telefones tipo: " + loJsonRecord.StringOf("contatos[j].telefones[k].tipo")
            ? "  telefones id: " + loJsonRecord.StringOf("contatos[j].telefones[k].id")
            k = k + 1
        ENDDO

        ? "  cargo: " + loJsonRecord.StringOf("contatos[j].cargo")

        lnNumEmails = loJsonRecord.SizeOfArray("contatos[j].emails")
        ? "  Number of emails: " + STR(lnNumEmails)
        k = 0
        DO WHILE k < lnNumEmails
            loJsonRecord.K = k
            ? "  emails email: " + loJsonRecord.StringOf("contatos[j].emails[k].email")
            ? "  emails tipo: " + loJsonRecord.StringOf("contatos[j].emails[k].tipo")
            ? "  emails id: " + loJsonRecord.StringOf("contatos[j].emails[k].id")
            k = k + 1
        ENDDO

        j = j + 1
    ENDDO

    RELEASE loJsonRecord
    i = i + 1
ENDDO

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

RELEASE loSb
RELEASE loJsonArray