Share this page

# lr_json_set_values

Changes values in a JSON object.

int lr_json_set_values( "JsonObject=<parameter name>", "Value or ValueParam=<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 query.
Value | The data to set
ValueParam | The name of a parameter containing the data to set. If `SelectAll = Yes`, pass a parameter array with the same number of values as the expected number of matches.
SelectAll | Optional: If Yes, finds all matches. If No, stops after first match. 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_set_values sets the values at the nodes that match the query to the specified value.

If a parameter array is passed as the `ValueParam` and `SelectAll = Yes`, the values in the array are used in the corresponding matches. That is, the first value in the parameter array is the new value for the first match, the second value is for the second match, and so on. If there are more matches than there are elements in the parameter array, the value replacement stops when all the array elements have been used. For example, if the parameter array contains 5 elements and there are 10 matches to the query, the first 5 matches have the values replaced with the 5 array elements, respectively, and the remaining matches are not changed.

If you want to change the key name as well as the value, use lr_json_replace.

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

Copy code

    TestSetGetValues()
    {
        int i = 0;

        // Test Set/Get Values

        i = lr_eval_json("Buffer/File=store.json", // See <MadCap:xref href="etc/lrFr_store_json.htm" xmlns:MadCap="http://www.madcapsoftware.com/Schemas/MadCap.xsd">File: store.json</MadCap:xref>
                         "JsonObject=s_json_obj", LAST);

        lr_json_stringify("JsonObject=s_json_obj",
                          "OutputParam=s_result",
                         LAST);

        // Single match
        i = lr_json_set_values("JsonObject=s_json_obj",
                        "Value=\"\Red Books\"",
                        "QueryString=$.store.name",
                         LAST);

        lr_json_stringify("JsonObject=s_json_obj",
                          "OutputParam=s_result",
                         LAST);

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

        // Test Get Values
        i = lr_json_get_values("JsonObject=s_json_obj",
                    "ValueParam=s_new_storename",
                    "QueryString=$.store.name",
                     LAST);

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

        // Multi match with SelectAll=No
        i = lr_json_set_values("JsonObject=s_json_obj",
                        "Value=\"Ellie\"",
                        "QueryString=$.store.books[*].author",
                         LAST);
        if (i != 1)
            lr_error_message("lr_json_set_values should return %d, but returns %d", 1, i);

        i = lr_json_get_values("JsonObject=s_json_obj",
                    "ValueParam=s_new_firstauthor",
                    "QueryString=$.store.books[*].author",
                     LAST);

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

        // Multi match with SelectAll=Yes
        lr_save_string("\"1111\"", "s_isbn_1");
        lr_save_string("\"2222\"", "s_isbn_2");
        lr_save_string("2", "s_isbn_count");

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

        i = lr_json_set_values("JsonObject=s_json_obj",
                        "ValueParam=s_isbn",
                        "QueryString={s_path_isbn}",
                        "SelectAll=Yes",
                         LAST);
        if (i != 2)
            lr_error_message("lr_json_set_values should return %d, but returns %d", 2, i);

        i = lr_json_get_values("JsonObject=s_json_obj",
                    "ValueParam=s_new_isbn",
                    "QueryString={s_path_isbn}",
                    "SelectAll=Yes",
                     LAST);
        if (i != 2)
            lr_error_message("lr_json_get_values should return %d, but returns %d", 2, i);

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