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

Delphi DLL 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
Secrets
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

 

 

 

(Delphi DLL) 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 for Delphi Downloads

Chilkat non-ActiveX DLL for Delphi

Chilkat ActiveX DLL for Delphi

* The examples here use the non-ActiveX DLL.

uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, JsonArray, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
json: HCkJsonObject;
success: Boolean;
listA: HCkJsonArray;
bNull: Boolean;
tickerObj: HCkJsonObject;
aa: HCkJsonArray;
aFruit: HCkJsonArray;
appleObj: HCkJsonObject;
pearObj: HCkJsonObject;

begin
json := CkJsonObject_Create();

// Load the JSON from a file.
success := CkJsonObject_LoadFile(json,'qa_data/json/modifySample.json');
if (success <> True) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(json));
    Exit;
  end;

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

// Get the "list" array:
listA := CkJsonObject_ArrayOf(json,'list');

// Modify values in the list.

// Change banana to plantain
success := CkJsonArray_SetStringAt(listA,0,'plantain');

// Change 12 to 24
success := CkJsonArray_SetIntAt(listA,1,24);

// Change true to false
success := CkJsonArray_SetBoolAt(listA,2,False);

// Is the 3rd item null?
bNull := CkJsonArray_IsNullAt(listA,3);

// Change "orange" to 32.
success := CkJsonArray_SetIntAt(listA,4,32);

// Change 12.5 to 31.2
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.
success := CkJsonArray_DeleteAt(listA,6);
success := CkJsonArray_AddObjectAt(listA,6);
tickerObj := CkJsonArray_ObjectAt(listA,6);
success := CkJsonObject_AddStringAt(tickerObj,-1,'ticker','GOOG');

CkJsonObject_Dispose(tickerObj);

// Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
success := CkJsonArray_DeleteAt(listA,7);
success := CkJsonArray_AddArrayAt(listA,7);
aa := CkJsonArray_ArrayAt(listA,7);
success := CkJsonArray_AddStringAt(aa,-1,'apple');
success := CkJsonArray_AddIntAt(aa,-1,22);
success := CkJsonArray_AddBoolAt(aa,-1,True);
success := CkJsonArray_AddNullAt(aa,-1);
success := CkJsonArray_AddNumberAt(aa,-1,'1080.25');
CkJsonArray_Dispose(aa);

CkJsonArray_Dispose(listA);

// Get the "fruit" array
aFruit := CkJsonObject_ArrayAt(json,0);

// Get the 1st element:
appleObj := CkJsonArray_ObjectAt(aFruit,0);

// Modify values by member name:
success := CkJsonObject_SetStringOf(appleObj,'fruit','fuji_apple');
success := CkJsonObject_SetIntOf(appleObj,'count',46);
success := CkJsonObject_SetBoolOf(appleObj,'fresh',False);
success := CkJsonObject_SetStringOf(appleObj,'extraInfo','developed by growers at the Tohoku Research Station in Fujisaki');

CkJsonObject_Dispose(appleObj);

// Modify values by index:
pearObj := CkJsonArray_ObjectAt(aFruit,1);
success := CkJsonObject_SetStringAt(pearObj,0,'bartlett_pear');
success := CkJsonObject_SetIntAt(pearObj,1,12);
success := CkJsonObject_SetBoolAt(pearObj,2,False);
success := CkJsonObject_SetStringAt(pearObj,3,'harvested in late August to early September');
CkJsonObject_Dispose(pearObj);

CkJsonArray_Dispose(aFruit);

CkJsonObject_putEmitCompact(json,False);
Memo1.Lines.Add(CkJsonObject__emit(json));

CkJsonObject_Dispose(json);

end;

 

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