Chilkat Examples

ChilkatHOMEAndroid™AutoItCC#C++Chilkat2-PythonCkPythonClassic ASPDataFlexDelphi DLLGoJavaJavaScriptNode.jsObjective-CPHP ExtensionPerlPowerBuilderPowerShellPureBasicRubySQL ServerSwiftTclUnicode CUnicode C++VB.NETVBScriptVisual Basic 6.0Visual FoxProXojo Plugin

Unicode C Examples
Web API Categories

AI
ASN.1
AWS KMS
AWS Misc
Amazon EC2
Amazon Glacier
Amazon S3
Amazon S3 (new)
Amazon SES
Amazon SNS
Amazon SQS
Apple Keychain
Async
Azure Cloud Storage
Azure Key Vault
Azure Service Bus
Azure Table Service
Base64
Box
CAdES
CSR
CSV
Cert Store
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)
JavaScript
MHT / HTML Email
MIME
Markdown
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
Regular Expressions
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
X
XAdES
XML
XML Digital Signatures
XMP
Zip
curl
uncategorized

 

 

 

(Unicode C) Call a JavaScript Function Returning an Array

See more JavaScript Examples
Demonstrates how to call a JavaScript function that returns an array.

Note: This example requires Chilkat v11.4.0 or greater.

Chilkat C/C++ Library Downloads

MS Visual C/C++

C++ Builder

Linux C/C++

Alpine Linux C/C++

MacOS C/C++

iOS C/C++

Android C/C++

MinGW C/C++

#include <C_CkStringBuilderW.h>
#include <C_CkJsW.h>
#include <C_CkJsonObjectW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkStringBuilderW sbScript;
    HCkJsW js;
    HCkJsonObjectW result;
    HCkJsonObjectW funcCall;
    int count;
    int i;

    success = FALSE;

    // ----------------------------------------------------------------------------------
    // The Javascript functions called in this example are shown at the bottom of this page.
    // -----------------------------------------------------------------------------------

    sbScript = CkStringBuilderW_Create();
    success = CkStringBuilderW_LoadFile(sbScript,L"js_function_returning_array.js",L"utf-8");
    if (success == FALSE) {
        wprintf(L"%s\n",CkStringBuilderW_lastErrorText(sbScript));
        CkStringBuilderW_Dispose(sbScript);
        return;
    }

    js = CkJsW_Create();

    result = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(result,FALSE);

    // Call Eval to add the function to the context's global object
    success = CkJsW_Eval(js,sbScript,result);
    if (success == FALSE) {
        // Examine the result for an exception.
        wprintf(L"%s\n",CkJsonObjectW_emit(result));

        // Also examine the LastErrorText.
        wprintf(L"%s\n",CkJsW_lastErrorText(js));
        CkStringBuilderW_Dispose(sbScript);
        CkJsW_Dispose(js);
        CkJsonObjectW_Dispose(result);
        return;
    }

    // ------------------------------------------------------------------------------
    // Call each function

    funcCall = CkJsonObjectW_Create();

    // Create JSON specifying the function name and arguments
    // The function has no arguments, so we only specify the name.

    CkJsonObjectW_UpdateString(funcCall,L"name",L"getDays");

    success = CkJsW_CallFunction(js,funcCall,result);
    if (success == FALSE) {
        // Examine the result for an exception.
        wprintf(L"%s\n",CkJsonObjectW_emit(result));

        // Also examine the LastErrorText.
        wprintf(L"%s\n",CkJsW_lastErrorText(js));
        CkStringBuilderW_Dispose(sbScript);
        CkJsW_Dispose(js);
        CkJsonObjectW_Dispose(result);
        CkJsonObjectW_Dispose(funcCall);
        return;
    }

    wprintf(L"%s\n",CkJsonObjectW_emit(result));

    // Output:
    // {
    //   "type": "array",
    //   "value": [
    //     "Monday",
    //     "Tuesday",
    //     "Wednesday",
    //     "Thursday",
    //     "Friday"
    //   ]
    // }

    // Access each array value..
    count = CkJsonObjectW_SizeOfArray(result,L"value");
    i = 0;
    while (i < count) {
        CkJsonObjectW_putI(result,i);
        wprintf(L"%s\n",CkJsonObjectW_stringOf(result,L"value[i]"));
        i = i + 1;
    }

    // ------------------------------------------------------------------------------
    // Call the getRange(start,end) function

    CkJsonObjectW_Clear(funcCall);
    CkJsonObjectW_UpdateString(funcCall,L"name",L"getRange");
    CkJsonObjectW_UpdateInt(funcCall,L"args[0]",14);
    CkJsonObjectW_UpdateInt(funcCall,L"args[1]",21);
    success = CkJsW_CallFunction(js,funcCall,result);
    wprintf(L"%s\n",CkJsonObjectW_emit(result));

    // Output:
    // {
    //   "type": "array",
    //   "value": [
    //     14,
    //     15,
    //     16,
    //     17,
    //     18,
    //     19,
    //     20,
    //     21
    //   ]
    // }

    count = CkJsonObjectW_SizeOfArray(result,L"value");
    i = 0;
    while (i < count) {
        CkJsonObjectW_putI(result,i);
        wprintf(L"%d\n",CkJsonObjectW_IntOf(result,L"value[i]"));
        i = i + 1;
    }

    // ------------------------------------------------------------------------------
    // Call the getEmployees() function

    CkJsonObjectW_Clear(funcCall);
    CkJsonObjectW_UpdateString(funcCall,L"name",L"getEmployees");
    success = CkJsW_CallFunction(js,funcCall,result);
    wprintf(L"%s\n",CkJsonObjectW_emit(result));

    // Output:
    // {
    //   "type": "array",
    //   "value": [
    //     {
    //       "id": 101,
    //       "name": "Alice",
    //       "role": "Dev"
    //     },
    //     {
    //       "id": 102,
    //       "name": "Bob",
    //       "role": "Manager"
    //     }
    //   ]
    // }

    count = CkJsonObjectW_SizeOfArray(result,L"value");
    i = 0;
    while (i < count) {
        CkJsonObjectW_putI(result,i);
        wprintf(L"name: %s\n",CkJsonObjectW_stringOf(result,L"value[i].name"));
        wprintf(L"role: %s\n",CkJsonObjectW_stringOf(result,L"value[i].role"));
        wprintf(L"id: %d\n",CkJsonObjectW_IntOf(result,L"value[i].id"));
        wprintf(L"\n");
        i = i + 1;
    }



    CkStringBuilderW_Dispose(sbScript);
    CkJsW_Dispose(js);
    CkJsonObjectW_Dispose(result);
    CkJsonObjectW_Dispose(funcCall);

    }


function getDays() {
    return ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"];
}

function getRange(start, end) {
    var arr = [];
    for (var i = start; i <= end; i++) {
        arr.push(i);
    }
    return arr;
}
// Usage: getRange(1, 5) -> [1, 2, 3, 4, 5]

function getEmployees() {
    return [
        { id: 101, name: "Alice", role: "Dev" },
        { id: 102, name: "Bob", role: "Manager" }
    ];
}
 

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