Share this page

# lr_json_replace

Replaces a node in a JSON object.

int lr_json_replace( "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 replaced.
Value | The key-value pair to replace the target of the QueryString. Both the key and the value of the node that matches the query are replaced. The key-value pair can be bracketed or not. For example: `"Value={\"color\":\"red\"}"` or `"Value=\"color\":\"red\""`.
ValueParam | The name of the parameter containing the key-value pair to replace the target of the QueryString.
JsonNode | The name of a parameter containing the handle of the JSON object created by lr_eval_json The object referenced by JsonNode replaces the QueryString match in 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_replace replaces the node or nodes specified by the query with the key-value pair passed in the Value, ValueParam, or JsonNode arguments.

Use lr_json_replace to change the name and value of the matched node. If you want to change the value but not the name, you can use lr_json_set_values.

If more than one key-value pair is passed in Value or ValueParam, only the first is used.

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_replace.

Copy code

    TestReplace()
    {
        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=r_json_obj", LAST);

        /* Change "name": "Black Books" to "owner":"Mr Black".*/
        i = lr_json_replace("JsonObject=r_json_obj",
                        "Value=\"\owner\":\"Mr Black\"",
                        "QueryString=$.store.name",
                         LAST);

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

        lr_save_string("$.store.books[*].isbn", "r_path_isbn");

        i = lr_json_replace("JsonObject=r_json_obj",
                        "Value=\"discount\":\"20 percent\"",
                        "QueryString={r_path_isbn}",
                        "SelectAll=Yes",
                         LAST);
        if (i != 2)
            lr_error_message("lr_json_replace should return %d, but returns %d", 2, i);

        lr_json_stringify("JsonObject=r_json_obj",
                          "OutputParam=r_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: