Utility Functions and C Language Reference > Utility Functions: C Language (lr_) > Alphabetical Listing > lr_json_stringify

lr_json_stringify

Converts a JSON object to a JSON string.

C Language

int lr_json_stringify( "JsonObject=<parameter name>", "Format=<auto|compact|indented>" "OutputParam=<parameter name>" );

Arguments

Name Comments
JsonObject The name of the parameter that stores the handle of the JSON object created by lr_eval_json.
Format Optional. The spacing and line return layout of the returned string. One of:
  • compact - No white space. Suitable for processing in code.
  • indented (Default) - Easier for user to read.
  • auto - Output is compact on the Controller and indented in the VuGen log.
OutputParam The name of a parameter to store the output string.

lr_json_stringify writes the string equivalent of a JSON object to a parameter. The object is not changed.

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

Return Values

This function returns 0 on success and -1 on failure.

Parameterization

Parameterization is not applicable to this function.

Example

This example shows the use of lr_json_stringify with different formats.

    char *json_input = 
        "{" 
            "\"cart\":" 
                "{" 
                    "\"customerId\":1343," 
                    "\"lineItems\":" 
                    "[ {\"productId\":12343,\"quantity\":4}, " 
                    "  {\"productId\":39293,\"quantity\":2} ]" 
                "}" 
            "}"; 
    lr_save_string (json_input, "JSON_Input"); 
    lr_eval_json ("Buffer={JSON_Input}", "JsonObject=JSON_Param");
    
    //compact
    lr_json_stringify("JsonObject=JSON_Param","Format=compact","OutputParam=Result",LAST );
    lr_output_message("Compact JSON is : %s", lr_eval_string("{Result}"));
    
    //indented
    lr_json_stringify("JsonObject=JSON_Param","Format=indented","OutputParam=Result",LAST );
    lr_output_message("Indented JSON is : %s", lr_eval_string("{Result}"));
    
    //auto
    lr_json_stringify("JsonObject=JSON_Param","Format=auto","OutputParam=Result",LAST );
    // Result is indented in VuGen and Compact on the Controller.
	
Output:

Action.c(21): Compact JSON is : {"cart":{"customerId":1343,"lineItems":[{"productId":12343,"quantity":4},{"productId":39293,"quantity":2}]}}

Action.c(25): Indented JSON is : {
    "cart": {
        "customerId": 1343,
        "lineItems": [
            {
                "productId": 12343,
                "quantity": 4
            },
            {
                "productId": 39293,
                "quantity": 2
            }
        ]
    }
}