Chilkat Examples

ChilkatHOMEAndroid™Classic ASPCC++C#Mono C#.NET Core C#C# UWP/WinRTDataFlexDelphi ActiveXDelphi DLLVisual FoxProJavaLianjaMFCObjective-CPerlPHP ActiveXPHP ExtensionPowerBuilderPowerShellPureBasicCkPythonChilkat2-PythonRubySQL ServerSwift 2Swift 3/4TclUnicode CUnicode C++Visual Basic 6.0VB.NETVB.NET UWP/WinRTVBScriptXojo PluginNode.jsExcelGo

Tcl Examples

Web API Categories

ASN.1
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Service Bus
Base64
Bounced Email
Box
CSR
CSV
Certificates
Compression
DKIM / DomainKey
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
ECC
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
Geolocation
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Photos
Google Sheets
Google Tasks
Gzip
HTML-to-XML/Text

HTTP
HTTP Misc
IMAP
JSON
JSON Web Encryption (JWE)
JSON Web Signatures (JWS)
JSON Web Token (JWT)
Java KeyStore (JKS)
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Socket/SSL/TLS
Spider
Stream
Tar Archive
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl

 

 

 

(Tcl) Loading and Parsing a Complex JSON Array

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

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

#  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.
set sb [new_CkStringBuilder]

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

#  Load the JSON array into a JsonArray:
set jsonArray [new_CkJsonArray]

set success [CkJsonArray_LoadSb $jsonArray $sb]
if {[expr $success != 1]} then {
    puts [CkJsonArray_lastErrorText $jsonArray]
    delete_CkStringBuilder $sb
    delete_CkJsonArray $jsonArray
    exit
}

#  Get some information from each record in the array.
set numRecords [CkJsonArray_get_Size $jsonArray]
set i 0
while {[expr $i < $numRecords]} {
    puts "------ Record $i -------"

    # jsonRecord is a CkJsonObject
    set jsonRecord [CkJsonArray_ObjectAt $jsonArray $i]

    #  Examine information for this record
    set numTelefones [CkJsonObject_SizeOfArray $jsonRecord "telefones"]
    puts "Number of telefones: $numTelefones"
    set j 0
    while {[expr $j < $numTelefones]} {
        CkJsonObject_put_J $jsonRecord $j
        puts "  telefones numero: [CkJsonObject_stringOf $jsonRecord telefones[j].numero]"
        puts "  telefones tipo: [CkJsonObject_stringOf $jsonRecord telefones[j].tipo]"
        puts "  telefones id: [CkJsonObject_stringOf $jsonRecord telefones[j].id]"
        set j [expr $j + 1]
    }

    puts "cnpj: [CkJsonObject_stringOf $jsonRecord cnpj]"
    puts "rua: [CkJsonObject_stringOf $jsonRecord rua]"
    #  ...

    set numContatos [CkJsonObject_SizeOfArray $jsonRecord "contatos"]
    puts "Number of contatos: $numContatos"
    set j 0
    while {[expr $j < $numContatos]} {
        CkJsonObject_put_J $jsonRecord $j

        set numTelefones [CkJsonObject_SizeOfArray $jsonRecord "contatos[j].telefones"]
        puts "  Number of telefones: $numTelefones"
        set k 0
        while {[expr $k < $numTelefones]} {
            CkJsonObject_put_K $jsonRecord $k
            puts "  telefones numero: [CkJsonObject_stringOf $jsonRecord contatos[j].telefones[k].numero]"
            puts "  telefones tipo: [CkJsonObject_stringOf $jsonRecord contatos[j].telefones[k].tipo]"
            puts "  telefones id: [CkJsonObject_stringOf $jsonRecord contatos[j].telefones[k].id]"
            set k [expr $k + 1]
        }

        puts "  cargo: [CkJsonObject_stringOf $jsonRecord contatos[j].cargo]"

        set numEmails [CkJsonObject_SizeOfArray $jsonRecord "contatos[j].emails"]
        puts "  Number of emails: $numEmails"
        set k 0
        while {[expr $k < $numEmails]} {
            CkJsonObject_put_K $jsonRecord $k
            puts "  emails email: [CkJsonObject_stringOf $jsonRecord contatos[j].emails[k].email]"
            puts "  emails tipo: [CkJsonObject_stringOf $jsonRecord contatos[j].emails[k].tipo]"
            puts "  emails id: [CkJsonObject_stringOf $jsonRecord contatos[j].emails[k].id]"
            set k [expr $k + 1]
        }

        set j [expr $j + 1]
    }

    delete_CkJsonObject $jsonRecord

    set i [expr $i + 1]
}

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

delete_CkStringBuilder $sb
delete_CkJsonArray $jsonArray

 

© 2000-2019 Chilkat Software, Inc. All Rights Reserved.