This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Replace Json object value with another Json

Can you please help how replace Json value with another Json.

{
"AppId": "1234",
"Name": null,
"Number": 9878,
"RequestBody": "123",
"Date": null
}

I want to replace RequestBody with other Json

{
"Test": null,
"Value": 9878,
"Date": null
}

Expecting output: 

{
"AppId": "1234",
"Name": null,
"Number": 9878,
"RequestBody": "{"Test": null,"Value": 9878,"Date": null}",
"Date": null
}

I tried with below

lr_eval_json("Buffer={ResponseBody}", "JsonObject=MJO",LAST);
    lr_json_stringify("JsonObject=MJO","Format=compact", "OutputParam=response",LAST);
    lr_save_string(lr_eval_string(lr_eval_string("{response}")),"tmp"); 

lr_eval_json("Buffer/File=payload.json",
                 "JsonObject=json_obj", LAST);

    
    lr_json_set_values("JsonObject=json_obj",
                   "Value={tmp}",
                   "QueryString=$.RequestBody",
                   "SelectAll=Yes",
                   LAST);
    
      lr_json_stringify("JsonObject=json_obj","Format=compact", "OutputParam=newJsonBody",LAST);
      
    lr_save_string(lr_eval_string(lr_eval_string("{newJsonBody}")),"body");

I am getting 

{
"AppId": "1234",
"Name": null,
"Number": 9878,
"RequestBody": {"Test": null,"Value": 9878,"Date": null},
"Date": null
}

Issue: Double quotes are missing at RequestBody value. If I place manually It is not taking as Json

Parents
  • Suggested Answer

    0  

    ,

    The last output shown is correct JSON and the expected output not. JSON is a nested data structure and each member like "RequestBody" can have a single value like a string, or number etc, but also a List (something between [ ] ) or even an Structure (or Dict) with members between { }.

    It you like to add to a JSON member a string, you need to escape each inner " with \ and might replace each newline with \n.

    So this is also OK (I left out the new lines):

    "RequestBody": "{\"Test\": null,\"Value\": 9878,\"Date\": null}"

    To get the last value in RequestBody your have to escape each " in the result of lr_eval_string("{response}") before assigning it to parameter tmp.

    I think also that you need quotes around the {tmp} in:

                   "Value=\"{tmp}\"",

    or make a json-stringify function that does all the work. E.g.

     // evaluate and stringify a json_param and puts it
    // as a string constant in param.
    void my_json_stringify(char *json_param, char *param)
    {
        char *p, *q;
        char *buf = (char*)malloc(strlen(json_param)+10);
        sprintf(buf,"Buffer={%s}", json_param);
        lr_eval_json(buf, "JsonObject=MY_MJO",LAST);
        free(buf);
        lr_json_stringify("JsonObject=MY_MJO","Format=compact", "OutputParam=my_json_stringify_param",LAST);
        p = lr_eval_string("{my_json_stringify_param}");
        buf = q = (char*)malloc(strlen(p)*2+2);
        *q++ = '"';                    // Start quote
        while(*p) {
            if( *p == '"' ) *q++ = '\\';  // Escape "
            *q++ = *p++;                  // Copy character
        }
        *q++ = '"';                    // End quote
        *q   = '\0';                   // End of String
        lr_save_string(buf, param);
        free(buf);
    }


    // Example:
    lr_save_string("{\"Test\": null,\n\"Value\": 9878,\n\"Date\": null\n}", "ResponseBody");
    my_json_stringify("ResponseBody", "response2");
    lr_log_message(lr_eval_string("{response2}"));
    // Log: "{\"Test\":null,\"Value\":9878,\"Date\":null}"

    How to ask questions

    Reward contributions via likes or 'verified answers'

Reply
  • Suggested Answer

    0  

    ,

    The last output shown is correct JSON and the expected output not. JSON is a nested data structure and each member like "RequestBody" can have a single value like a string, or number etc, but also a List (something between [ ] ) or even an Structure (or Dict) with members between { }.

    It you like to add to a JSON member a string, you need to escape each inner " with \ and might replace each newline with \n.

    So this is also OK (I left out the new lines):

    "RequestBody": "{\"Test\": null,\"Value\": 9878,\"Date\": null}"

    To get the last value in RequestBody your have to escape each " in the result of lr_eval_string("{response}") before assigning it to parameter tmp.

    I think also that you need quotes around the {tmp} in:

                   "Value=\"{tmp}\"",

    or make a json-stringify function that does all the work. E.g.

     // evaluate and stringify a json_param and puts it
    // as a string constant in param.
    void my_json_stringify(char *json_param, char *param)
    {
        char *p, *q;
        char *buf = (char*)malloc(strlen(json_param)+10);
        sprintf(buf,"Buffer={%s}", json_param);
        lr_eval_json(buf, "JsonObject=MY_MJO",LAST);
        free(buf);
        lr_json_stringify("JsonObject=MY_MJO","Format=compact", "OutputParam=my_json_stringify_param",LAST);
        p = lr_eval_string("{my_json_stringify_param}");
        buf = q = (char*)malloc(strlen(p)*2+2);
        *q++ = '"';                    // Start quote
        while(*p) {
            if( *p == '"' ) *q++ = '\\';  // Escape "
            *q++ = *p++;                  // Copy character
        }
        *q++ = '"';                    // End quote
        *q   = '\0';                   // End of String
        lr_save_string(buf, param);
        free(buf);
    }


    // Example:
    lr_save_string("{\"Test\": null,\n\"Value\": 9878,\n\"Date\": null\n}", "ResponseBody");
    my_json_stringify("ResponseBody", "response2");
    lr_log_message(lr_eval_string("{response2}"));
    // Log: "{\"Test\":null,\"Value\":9878,\"Date\":null}"

    How to ask questions

    Reward contributions via likes or 'verified answers'

Children
  • 0 in reply to   
    constant

    Thank you for the idea. I used the below code to replace " with /".

    char *string_replace(char *input_string,
        char *substring_to_be_replaced,
        char *substitution_string)

     {

    // strstr function declaration
     char *strstr(const char *s1, const char *s2); 

    // newstring variable the new string to be returned
    // increase the buffer size if needed. Default is 200

    char newstring[200]=""; 

    // str is a temporary pointer to aid in creation of newstring;
    // it points at the beginning of each substring that
    // needs to be included in the new string.

    char *str; 

    // ptr is a pointer to hold strstr result.

    char *ptr; 

    str = input_string;

    // search for substring_to_be_replaced and
    // replace with substitution_string

     while((ptr = strstr(input_string, substring_to_be_replaced))!=NULL)
     {
     input_string = &ptr[strlen(substring_to_be_replaced)];
     ptr[0] = '\0';
     strcat(newstring, str);
     strcat(newstring, substitution_string);
     str = input_string;
     }

    // concatenate the rest of the string

    strcat(newstring, input_string);

    return newstring;

     }