Forum Discussion

Squeak's avatar
Squeak
Icon for Cirrus rankCirrus
May 26, 2020

Parse username from server response

Hi,

I´m trying to parse a username from a response that I´m receiving from a external server.

The complete response from the server·

HTTP/1.1 200 OK  Cache-Control: private  Content-Type: application/json; charset=utf-8  Server: Microsoft-IIS/10.0  X-AspNet-Version: 4.0.30319  X-Powered-By: ASP.NET  Date: Mon, 25 May 2020 10:18:21 GMT  Content-Length: 47    {"pid":"asdf001","mobileNumber":"+12345678909"}

I´m using "findstr" to search for the username.

set parse_user [findstr $recv_data "pid" 5 ","]
log local0. " status ---->>>$parse_user"

The log local0. looks this

"asdf001"

My question is, how do I remove the quotes on both sides of the username?

Thanks in advance.

  • Hi Squeak,

     

    You can use string map to strip the double quotes.

    set strip_quotes [string map {\" {}} $parse_user]

     

    Kind regards

    Ben

  • Hi Squeak,

     

    You can use string map to strip the double quotes.

    set strip_quotes [string map {\" {}} $parse_user]

     

    Kind regards

    Ben

    • Squeak's avatar
      Squeak
      Icon for Cirrus rankCirrus

      Hi,

       

      I tried your solution and it worked!

      Can you describe how this syntax {\" {}} removed the double quotes?

       

      Thank you.

  • Hi Squeak,

    http://www.tcl.tk/man/tcl8.5/TclCmd/Tcl.htm#M24

    "If a backslash (“\”) appears within a word then backslash substitution occurs." -> "This allows characters such as double quotes, close brackets, and dollar signs to be included in words without triggering special processing."

    In this example, we've substituted double quotes with an empty space {}. You can alternatively use double quotes for the second parameter:

    set strip_quotes [string map {\" ""} $parse_user]

    Kind regards

    Ben