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
CAdES
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) Modify Parts of JSON Document

Demonstrates how to modify parts of a JSON document. This example uses the following JSON document:

{
   "fruit": [
      	{
         "kind": "apple",
	 "count": 24,
	 "fresh": true,
	 "extraInfo": null,
	 "listA": [ "abc", 1, null, false ],
	 "objectB": { "animal" : "monkey" }
      	},
	{
         "kind": "pear",
	 "count": 18,
	 "fresh": false,
	 "extraInfo": null
	 "listA": [ "xyz", 24, null, true ],
	 "objectB": { "animal" : "lemur" }
	}
    ],
    "list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ],
    "alien" : true
}

Chilkat Tcl Extension Downloads

Chilkat Tcl Extension Downloads

load ./chilkat.dll

set json [new_CkJsonObject]

#  Load the JSON from a file.
set success [CkJsonObject_LoadFile $json "qa_data/json/modifySample.json"]
if {[expr $success != 1]} then {
    puts [CkJsonObject_lastErrorText $json]
    delete_CkJsonObject $json
    exit
}

#  This example will not check for errors (i.e. null / false / 0 return values)...

#  Get the "list" array:
# listA is a CkJsonArray
set listA [CkJsonObject_ArrayOf $json "list"]

#  Modify values in the list.

#  Change banana to plantain
set success [CkJsonArray_SetStringAt $listA 0 "plantain"]

#  Change 12 to 24
set success [CkJsonArray_SetIntAt $listA 1 24]

#  Change true to false
set success [CkJsonArray_SetBoolAt $listA 2 0]

#  Is the 3rd item null?
set bNull [CkJsonArray_IsNullAt $listA 3]

#  Change "orange" to 32.
set success [CkJsonArray_SetIntAt $listA 4 32]

#  Change 12.5 to 31.2
set success [CkJsonArray_SetNumberAt $listA 5 "31.2"]

#  Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
#  Do this by deleting, then inserting a new object at the same location.
set success [CkJsonArray_DeleteAt $listA 6]
set success [CkJsonArray_AddObjectAt $listA 6]
# tickerObj is a CkJsonObject
set tickerObj [CkJsonArray_ObjectAt $listA 6]
set success [CkJsonObject_AddStringAt $tickerObj -1 "ticker" "GOOG"]

delete_CkJsonObject $tickerObj

#  Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
set success [CkJsonArray_DeleteAt $listA 7]
set success [CkJsonArray_AddArrayAt $listA 7]
# aa is a CkJsonArray
set aa [CkJsonArray_ArrayAt $listA 7]
set success [CkJsonArray_AddStringAt $aa -1 "apple"]
set success [CkJsonArray_AddIntAt $aa -1 22]
set success [CkJsonArray_AddBoolAt $aa -1 1]
set success [CkJsonArray_AddNullAt $aa -1]
set success [CkJsonArray_AddNumberAt $aa -1 "1080.25"]
delete_CkJsonArray $aa

delete_CkJsonArray $listA

#  Get the "fruit" array
# aFruit is a CkJsonArray
set aFruit [CkJsonObject_ArrayAt $json 0]

#  Get the 1st element:
# appleObj is a CkJsonObject
set appleObj [CkJsonArray_ObjectAt $aFruit 0]

#  Modify values by member name:
set success [CkJsonObject_SetStringOf $appleObj "fruit" "fuji_apple"]
set success [CkJsonObject_SetIntOf $appleObj "count" 46]
set success [CkJsonObject_SetBoolOf $appleObj "fresh" 0]
set success [CkJsonObject_SetStringOf $appleObj "extraInfo" "developed by growers at the Tohoku Research Station in Fujisaki"]

delete_CkJsonObject $appleObj

#  Modify values by index:
# pearObj is a CkJsonObject
set pearObj [CkJsonArray_ObjectAt $aFruit 1]
set success [CkJsonObject_SetStringAt $pearObj 0 "bartlett_pear"]
set success [CkJsonObject_SetIntAt $pearObj 1 12]
set success [CkJsonObject_SetBoolAt $pearObj 2 0]
set success [CkJsonObject_SetStringAt $pearObj 3 "harvested in late August to early September"]
delete_CkJsonObject $pearObj

delete_CkJsonArray $aFruit

CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]

delete_CkJsonObject $json

 

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