JSON Decoding Functions(4.2)

The functions described in this section are used to decode JSON formatted strings.

jsonDecodeUdr

This function decodes a JSON formatted string to a DRUDR.

void jsonDecodeUdr ( string jsonString,
                     DRUDR udr,                                    
                     boolean requireExactMapping (optional) )   
ParameterDescription

jsonString

The JSON string to decode

udrThe UDR to store the decoded data
requireExactMapping

Set to true if there must be matching UDR fields for all fields in the JSON string. The default value is false.

When requireExactMapping is set to true and the function fails to match a field, it will throw an exception. If the function is used in batch workflow, the exception will cause it to abort. In a real-time workflow, the function does not decode the string, but the workflow does not abort.

When requireExactMapping is set to false, the function will decode all JSON fields that can be mapped to a UDR field, the rest of the string will be ignored.

Returns

Nothing

Example - Encoding UDR to JSON and decoding the result

 Ultra:

internal itemUDR {
    int itId;
    string itDescription;      
    string itMisc1 : optional;
    list<string> itMisc2 : optional;   
};

internal transactionUDR {
    int trId ;
    string trDescription;
    float amount;
    long trDate;
    map<string, itemUDR> trItems;
    boolean trProcessed;
    ipaddress trSourceIP;  
    
};


APL:

import ultra.JSON_EXAMPLE.ULTRA_JSON_EXAMPLE;

consume {  
    //Item 1
    itemUDR item1 = udrCreate(itemUDR);
    item1.itId = 1;
    item1.itDescription = "Item1";  
    item1.itMisc2 = listCreate(string);
    listAdd(item1.itMisc2, "abc");
    listAdd(item1.itMisc2, "def");
    listAdd(item1.itMisc2, "ghi"); 
     
    //Item2
    itemUDR item2 = udrCreate(itemUDR);
    item2.itId = 1;
    item2.itDescription = "Item2";
    item2.itMisc1 = "abc";       
      
    //Transaction1        
    transactionUDR transaction1  = udrCreate(transactionUDR);
    transaction1.trId = 1;
    transaction1.trDescription = "Transaction1";
    transaction1.amount = 999.99;
    transaction1.trDate = dateCreateNowMilliseconds();
    transaction1.trItems = mapCreate(string, itemUDR);
    mapSet(transaction1.trItems, "item1Key",item1);  
    mapSet(transaction1.trItems, "item2Key",item1);  
    transaction1.trProcessed = true; 
    transaction1.trSourceIP = ipLocalHost();
     
    //Encode with JSON
    string json;
    json = jsonEncodeUdr(transaction1);        
    bytearray ba;
    strToBA(ba, json); 
    debug("Encoded JSON (" + baSize(ba) + "bytes):\n" + json + "\n" ); 
        
    //Decode from JSON
    transactionUDR transactionIn = udrCreate(transactionUDR);        
    jsonDecodeUdr(json, transactionIn);       
    debug("Decoded JSON:\n" + transactionIn);       


jsonParse

This function decodes a JSON formatted string to an any.

any jsonParse(string jsonString)   
ParameterDescription

jsonString

The JSON string to decode

Returns

any

Example - Decoding JSON

consume {
    // Define json with a multi-line string literal
    string json = """
        {
            "menu": {
                "header": "Document Viewer",
                "items": [
                    {"id": "Open"},
                    {"id": "OpenNew", "label": "Open New"},
                    null,
                    {"id": "ZoomIn", "label": "Zoom In"},
                    {"id": "ZoomOut", "label": "Zoom Out"},
                    {"id": "OriginalView", "label": "Original View"}
                ]
            }
        }
    """;

    // Parse the json to get an object
    any obj = jsonParse(json);
    debug(obj);

    // Access the object values as fields
    for (any item : obj.menu.items) {
        if (item != null) {
	        debug("label for " + item.id + " is " + item.label);
        }
    }