Forum Discussion

David_Simmons's avatar
David_Simmons
Icon for Nimbostratus rankNimbostratus
Mar 30, 2018

string map with variables including quotes

I'm trying to do some irule manipulation of JSON payload data and in specific cases add a couple of fields to the payload. At this point, I'm simply logging my changes but once I validate that this is working I plan to replace the payload data with my updated JSON data. Here is my example:

when HTTP_REQUEST_DATA {
    set searchvalue "\"Phone\":\"7345551212\","
    set replacevalue "\"\\Phone\":\"7345551212\",\"TestData\":\"Yes\","
     do stuff with the payload
    set payload [HTTP::payload]
    if { ${payload} contains ${searchvalue} } {
        set collectdata 1
        set payload [string map "${searchvalue} ${replacevalue}" ${payload}]
        log local0. "${payload}"
    }
}

The issue I'm running into is that the string map command is expanding my variables and then getting confused by the quoting. Is there any way to do this and make it work with variables containing quotes?

2 Replies

  • Can you give an example on what you are expecting but what you are receiving ? Usually, when you run into quotation issues, you can try to use single quotes (') and double quotes (") in combination.

     

  • Look at this in tclsh (shell available on most of linux OS and also on bigip)

     

    Your command :

     

     

    $ tclsh
    % set payload "\{\"parameter1\":\"value1\",\"Phone\":\"7345551212\",\"parameter2\":\"value2\"\}"
    {"parameter1":"value1","Phone":"7345551212","parameter2":"value2"}
    % set searchvalue "\"Phone\":\"7345551212\","
    "Phone":"7345551212",
    % set replacevalue "\"Phone\":\"7345551212\",\"TestData\":\"Yes\","
    "Phone":"7345551212","TestData":"Yes",
    % string map "${searchvalue} ${replacevalue}" ${payload}
    list element in quotes followed by ":"7345551212"," instead of space
    

     

    with search / replace strings between curly brackets:

     

     

    $ tclsh
    % set payload "\{\"parameter1\":\"value1\",\"Phone\":\"7345551212\",\"parameter2\":\"value2\"\}"
    {"parameter1":"value1","Phone":"7345551212","parameter2":"value2"}
    % set searchvalue {\"Phone\":\"7345551212\",}
    \"Phone\":\"7345551212\",
    % set replacevalue {\"Phone\":\"7345551212\",\"TestData\":\"Yes\",}
    \"Phone\":\"7345551212\",\"TestData\":\"Yes\",
    % string map "${searchvalue} ${replacevalue}" ${payload}
    {"parameter1":"value1","Phone":"7345551212","TestData":"Yes","parameter2":"value2"}
    

     

    or with string map list created before (with curly brackets)

     

     

    $ tclsh
    % set payload "\{\"parameter1\":\"value1\",\"Phone\":\"7345551212\",\"parameter2\":\"value2\"\}"
    {"parameter1":"value1","Phone":"7345551212","parameter2":"value2"}
    % set stringmap {"\"Phone\":\"7345551212\"," "\"Phone\":\"7345551212\",\"TestData\":\"Yes\","}
    "\"Phone\":\"7345551212\"," "\"Phone\":\"7345551212\",\"TestData\":\"Yes\","
    % string map $stringmap ${payload}
    {"parameter1":"value1","Phone":"7345551212","TestData":"Yes","parameter2":"value2"}
    

     

    or with string map list created before (with list command)

     

     

    $ tclsh
    % set payload "\{\"parameter1\":\"value1\",\"Phone\":\"7345551212\",\"parameter2\":\"value2\"\}"
    {"parameter1":"value1","Phone":"7345551212","parameter2":"value2"}
    set stringmap [list "\"Phone\":\"7345551212\"," "\"Phone\":\"7345551212\",\"TestData\":\"Yes\","]
    {"Phone":"7345551212",} {"Phone":"7345551212","TestData":"Yes",}
    % string map $stringmap ${payload}
    {"parameter1":"value1","Phone":"7345551212","TestData":"Yes","parameter2":"value2"}
    % 
    

     

    or with string map list created before (from searchvalue variable)

     

     

    $ tclsh
    % set payload "\{\"parameter1\":\"value1\",\"Phone\":\"7345551212\",\"parameter2\":\"value2\"\}"
    {"parameter1":"value1","Phone":"7345551212","parameter2":"value2"}
    % set searchvalue "\"Phone\":\"7345551212\","
    "Phone":"7345551212",
    % set stringmap [list $searchvalue $searchvalue"\"Phone\":\"7345551212\",\"TestData\":\"Yes\","]
    {"Phone":"7345551212",} {"Phone":"7345551212",""Phone":"7345551212","TestData":"Yes","}
    % string map $stringmap ${payload}
    {"parameter1":"value1","Phone":"7345551212",""Phone":"7345551212","TestData":"Yes",""parameter2":"value2"}