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

Node.js Examples

ASN.1
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
Facebook
FileAccess
Firebase
GMail REST API
GeoOp
Google APIs
Google Calendar
Google Cloud Storage
Google Drive
Google Sheets
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)
Jira
MHT / HTML Email
MIME
Microsoft Graph
NTLM
OAuth1
OAuth2
OneDrive
OpenSSL
Outlook
PEM
PFX/P12
POP3
PRNG
PayPal
Peoplevox
QuickBooks
REST
REST Misc
RSA Encryption
SCP
SFTP
SMTP
SSH
SSH Key
SSH Tunnel
SharePoint
Shopify
Socket/SSL/TLS
Spider
Stream
Stripe
SugarCRM
Tar Archive
Twitter
Upload
VoiceBase
Walmart
WebSocket
XAdES
XML
XML Digital Signatures
XMP
Xero
Zip
curl
eBay

 

 

 

(Node.js) 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
}

Install Chilkat for Node.js and Electron using npm at

Chilkat npm packages for Node.js

Chilkat npm packages for Electron

on Windows, Linux, MacOSX, and ARM

var os = require('os');
if (os.platform() == 'win32') {  
    if (os.arch() == 'ia32') {
        var chilkat = require('@chilkat/ck-node11-win-ia32');
    } else {
        var chilkat = require('@chilkat/ck-node11-win64'); 
    }
} else if (os.platform() == 'linux') {
    if (os.arch() == 'arm') {
        var chilkat = require('@chilkat/ck-node11-arm');
    } else if (os.arch() == 'x86') {
        var chilkat = require('@chilkat/ck-node11-linux32');
    } else {
        var chilkat = require('@chilkat/ck-node11-linux64');
    }
} else if (os.platform() == 'darwin') {
    var chilkat = require('@chilkat/ck-node11-macosx');
}

function chilkatExample() {

    var json = new chilkat.JsonObject();

    // Load the JSON from a file.
    var success = json.LoadFile("qa_data/json/modifySample.json");
    if (success !== true) {
        console.log(json.LastErrorText);
        return;
    }

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

    // Get the "list" array:
    // listA: JsonArray
    var listA = json.ArrayOf("list");

    // Modify values in the list.

    // Change banana to plantain
    success = listA.SetStringAt(0,"plantain");

    // Change 12 to 24
    success = listA.SetIntAt(1,24);

    // Change true to false
    success = listA.SetBoolAt(2,false);

    // Is the 3rd item null?
    var bNull = listA.IsNullAt(3);

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

    // Change 12.5 to 31.2
    success = listA.SetNumberAt(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 = listA.DeleteAt(6);
    success = listA.AddObjectAt(6);
    // tickerObj: JsonObject
    var tickerObj = listA.ObjectAt(6);
    success = tickerObj.AddStringAt(-1,"ticker","GOOG");

    // Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
    success = listA.DeleteAt(7);
    success = listA.AddArrayAt(7);
    // aa: JsonArray
    var aa = listA.ArrayAt(7);
    success = aa.AddStringAt(-1,"apple");
    success = aa.AddIntAt(-1,22);
    success = aa.AddBoolAt(-1,true);
    success = aa.AddNullAt(-1);
    success = aa.AddNumberAt(-1,"1080.25");

    // Get the "fruit" array
    // aFruit: JsonArray
    var aFruit = json.ArrayAt(0);

    // Get the 1st element:
    // appleObj: JsonObject
    var appleObj = aFruit.ObjectAt(0);

    // Modify values by member name:
    success = appleObj.SetStringOf("fruit","fuji_apple");
    success = appleObj.SetIntOf("count",46);
    success = appleObj.SetBoolOf("fresh",false);
    success = appleObj.SetStringOf("extraInfo","developed by growers at the Tohoku Research Station in Fujisaki");

    // Modify values by index:
    // pearObj: JsonObject
    var pearObj = aFruit.ObjectAt(1);
    success = pearObj.SetStringAt(0,"bartlett_pear");
    success = pearObj.SetIntAt(1,12);
    success = pearObj.SetBoolAt(2,false);
    success = pearObj.SetStringAt(3,"harvested in late August to early September");

    json.EmitCompact = false;
    console.log(json.Emit());

}

chilkatExample();

 

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