Chilkat Examples

ChilkatHOME.NET Core C#Android™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi ActiveXDelphi DLLGoJavaLianjaMono C#Node.jsObjective-CPHP ActiveXPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwift 2Swift 3,4,5...TclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

DataFlex Examples

Web API Categories

ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Bounced Email
Box
CAdES
CSR
CSV
Certificates
Cloud Signature CSC
Code Signing
Compression
DKIM / DomainKey
DNS
DSA
Diffie-Hellman
Digital Signatures
Dropbox
Dynamics CRM
EBICS
ECC
Ed25519
Email Object
Encryption
FTP
FileAccess
Firebase
GMail REST API
GMail SMTP/IMAP/POP
Geolocation
Google APIs
Google Calendar
Google Cloud SQL
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
MS Storage Providers
Microsoft Graph
Misc
NTLM
OAuth1
OAuth2
OIDC
Office365
OneDrive
OpenSSL
Outlook
Outlook Calendar
Outlook Contact
PDF Signatures
PEM
PFX/P12
PKCS11
POP3
PRNG
REST
REST Misc
RSA
SCP
SCard
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
ScMinidriver
SharePoint
SharePoint Online
Signing in the Cloud
Socket/SSL/TLS
Spider
Stream
Tar Archive
ULID/UUID
Upload
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(DataFlex) 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 ActiveX Downloads

ActiveX for 32-bit and 64-bit Windows

Use ChilkatAx-win32.pkg

Procedure Test
    Handle hoJson
    Boolean iSuccess
    Variant vListA
    Handle hoListA
    Boolean iBNull
    Variant vTickerObj
    Handle hoTickerObj
    Variant vAa
    Handle hoAa
    Variant vAFruit
    Handle hoAFruit
    Variant vAppleObj
    Handle hoAppleObj
    Variant vPearObj
    Handle hoPearObj
    String sTemp1

    Get Create (RefClass(cComChilkatJsonObject)) To hoJson
    If (Not(IsComObjectCreated(hoJson))) Begin
        Send CreateComObject of hoJson
    End

    // Load the JSON from a file.
    Get ComLoadFile Of hoJson "qa_data/json/modifySample.json" To iSuccess
    If (iSuccess <> True) Begin
        Get ComLastErrorText Of hoJson To sTemp1
        Showln sTemp1
        Procedure_Return
    End

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

    // Get the "list" array:
    Get ComArrayOf Of hoJson "list" To vListA
    If (IsComObject(vListA)) Begin
        Get Create (RefClass(cComChilkatJsonArray)) To hoListA
        Set pvComObject Of hoListA To vListA
    End

    // Modify values in the list.

    // Change banana to plantain
    Get ComSetStringAt Of hoListA 0 "plantain" To iSuccess

    // Change 12 to 24
    Get ComSetIntAt Of hoListA 1 24 To iSuccess

    // Change true to false
    Get ComSetBoolAt Of hoListA 2 False To iSuccess

    // Is the 3rd item null?
    Get ComIsNullAt Of hoListA 3 To iBNull

    // Change "orange" to 32.
    Get ComSetIntAt Of hoListA 4 32 To iSuccess

    // Change 12.5 to 31.2
    Get ComSetNumberAt Of hoListA 5 "31.2" To iSuccess

    // Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
    // Do this by deleting, then inserting a new object at the same location.
    Get ComDeleteAt Of hoListA 6 To iSuccess
    Get ComAddObjectAt Of hoListA 6 To iSuccess
    Get ComObjectAt Of hoListA 6 To vTickerObj
    If (IsComObject(vTickerObj)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoTickerObj
        Set pvComObject Of hoTickerObj To vTickerObj
    End
    Get ComAddStringAt Of hoTickerObj -1 "ticker" "GOOG" To iSuccess

    Send Destroy of hoTickerObj

    // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
    Get ComDeleteAt Of hoListA 7 To iSuccess
    Get ComAddArrayAt Of hoListA 7 To iSuccess
    Get ComArrayAt Of hoListA 7 To vAa
    If (IsComObject(vAa)) Begin
        Get Create (RefClass(cComChilkatJsonArray)) To hoAa
        Set pvComObject Of hoAa To vAa
    End
    Get ComAddStringAt Of hoAa -1 "apple" To iSuccess
    Get ComAddIntAt Of hoAa -1 22 To iSuccess
    Get ComAddBoolAt Of hoAa -1 True To iSuccess
    Get ComAddNullAt Of hoAa -1 To iSuccess
    Get ComAddNumberAt Of hoAa -1 "1080.25" To iSuccess
    Send Destroy of hoAa

    Send Destroy of hoListA

    // Get the "fruit" array
    Get ComArrayAt Of hoJson 0 To vAFruit
    If (IsComObject(vAFruit)) Begin
        Get Create (RefClass(cComChilkatJsonArray)) To hoAFruit
        Set pvComObject Of hoAFruit To vAFruit
    End

    // Get the 1st element:
    Get ComObjectAt Of hoAFruit 0 To vAppleObj
    If (IsComObject(vAppleObj)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoAppleObj
        Set pvComObject Of hoAppleObj To vAppleObj
    End

    // Modify values by member name:
    Get ComSetStringOf Of hoAppleObj "fruit" "fuji_apple" To iSuccess
    Get ComSetIntOf Of hoAppleObj "count" 46 To iSuccess
    Get ComSetBoolOf Of hoAppleObj "fresh" False To iSuccess
    Get ComSetStringOf Of hoAppleObj "extraInfo" "developed by growers at the Tohoku Research Station in Fujisaki" To iSuccess

    Send Destroy of hoAppleObj

    // Modify values by index:
    Get ComObjectAt Of hoAFruit 1 To vPearObj
    If (IsComObject(vPearObj)) Begin
        Get Create (RefClass(cComChilkatJsonObject)) To hoPearObj
        Set pvComObject Of hoPearObj To vPearObj
    End
    Get ComSetStringAt Of hoPearObj 0 "bartlett_pear" To iSuccess
    Get ComSetIntAt Of hoPearObj 1 12 To iSuccess
    Get ComSetBoolAt Of hoPearObj 2 False To iSuccess
    Get ComSetStringAt Of hoPearObj 3 "harvested in late August to early September" To iSuccess
    Send Destroy of hoPearObj

    Send Destroy of hoAFruit

    Set ComEmitCompact Of hoJson To False
    Get ComEmit Of hoJson To sTemp1
    Showln sTemp1


End_Procedure

 

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