Share this page

# lr_json_insert

Inserts a node in a JSON object.

int lr_json_insert( "JsonObject=<parameter name>", "Value or ValueParam or JsonNode=<value>", "QueryString=<query>", ["SelectAll=<Yes|No>",] ["NotFound=<Error|Continue>",] LAST );

Alphabetical Listing - C Language Utility Functions

## Arguments

Name | Comments
--- | ---
JsonObject | The name of the parameter that stores the handle of the JSON object created by lr_eval_json.
QueryString | The JSON path of the node to be inserted.
Value | The string to insert. If the target node type is array, the value must be parsible into a JSON array. If the target node type is object, the value must be parsible into a JSON object.
ValueParam | The name of the parameter containing the string to insert.
JsonNode | The name of a parameter containing the handle of the JSON object created by lr_eval_json The object referenced by JsonNode is inserted into the object referenced by JsonObject.
SelectAll | Optional: If SelectAll=Yes, the Value is inserted at every location that matches the query. If SelectAll=No, the Value is inserted at the first location that matches the query. Default is No
NotFound | Optional. One of: Error - Script run is aborted and error message is sent. (Default) Continue - Script run continues. A warning message is sent.
LAST | Required marker for the end of the argument list.

lr_json_insert inserts the value in the JSON object at the location specified by the query.

This function is not recorded. You can insert it manually into your script.

## Return Values

The number of matches.

## Parameterization

Parameterization is not applicable to this function.

## Example

This script shows the usage of lr_json_insert.

Copy code

    TestInsert()
    {
        int i = 0;

        // See <MadCap:xref href="etc/lrFr_store_json.htm" xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd">File: store.json</MadCap:xref>
        lr_eval_json("Buffer/File=store.json",
                     "JsonObject=i_json_obj", LAST);
        // 1 match
        i = lr_json_insert("JsonObject=i_json_obj",
                        "Value={\"owner\":\"Mr Black\"}",
                        "QueryString=$.store",
                         LAST);
        if (i != 1)
            lr_error_message("lr_json_insert should return %d, but returns %d", 1, i);

        // 1 match with JsonNode
        lr_save_string("{\"isbn\": \"0-048-08048-9\"}", "i_new_isbn");

        lr_eval_json("Buffer={i_new_isbn}",
                     "JsonObject=i_json_obj2", LAST);

        i = lr_json_insert("JsonObject=i_json_obj",
                           "JsonNode=i_json_obj2",
                           "QueryString=$.store.books[0]",
                           LAST);
        if (i != 1)
            lr_error_message("lr_json_insert should return %d, but returns %d", 1, i);

        // 2 match with ValueParam and SelectAll=Yes
        lr_save_string("{\"discount\":\"20 percent\"}", "i_new_discount");

        i = lr_json_insert("JsonObject=i_json_obj",
                           "ValueParam=i_new_discount",
                           "QueryString=$.store.books[?(@.price > 10)]",
                           "SelectAll=Yes",
                           LAST);
        if (i != 2)
            lr_error_message("lr_json_insert should return %d, but returns %d", 1, i);

        // 1 match
        // Notice that both of  the new items are inserted into the target array
        i = lr_json_insert("JsonObject=i_json_obj",
                        "Value=[{\"title\":\"New Book I\"}, {\"title\": \"New Book II\"}]",
                        "QueryString=$.store.books",
                         LAST);

        if (i != 1)
            lr_error_message("lr_json_insert should return %d, but returns %d", 1, i);

        lr_json_stringify("JsonObject=i_json_obj",
                          "OutputParam=i_result",
                         LAST);

        return 0;
    }

Tips to improve your search results... VuGen Function Reference Version 26.1-26.3 Function Reference 26.1-26.3 All View Local Help Center View Online Help Center Version: