Layer7 API Management

 View Only
  • 1.  Re: How to convert an x-www-form-urlencoded string to JSON?

    Posted Dec 16, 2019 12:14 PM
    Hello All,

    I'm trying to convert X-www-form-urlencoded string to JSON message and pass over to backend. Please find the sample message below

    ToCountry=US&ToState=VT&SmsMessageSid=SM432455e26341f85176300721a9689dca&NumMedia=0&ToCity=NORTON&FromZip=07662&SmsSid=SM432455e26341f85176300721a9689dca&FromState=NJ&SmsStatus=received&FromCity=ROCHELLE+PARK&Body=Hello&FromCountry=US&To=%2B18024350042&ToZip=05907&NumSegments=1&MessageSid=SM432455e26341f85176300721a9689dca&From=%2B12012537162&ApiVersion=2010-04-01

    Could anyone please help me with that.

    Regards,
    Prem.



  • 2.  RE: Re: How to convert an x-www-form-urlencoded string to JSON?
    Best Answer

    Posted Dec 16, 2019 12:42 PM
    I am not sure if there is an out-of-the box solution, but you can use a regex to replace the url-encoded delimiter (&) with the json delimiter (","). then another regex to replace the url-encoded key/val assignment (=) with ":". your string will look like this:

    ToCountry""US","ToState""VT","SmsMessageSid""SM432455e26341f85176300721a9689dca","NumMedia""0","ToCity""NORTON

    use the "set context variable" assertion to append the beginning {" and ending "}





    ------------------------------
    Software Developer II
    FedEx Services
    ------------------------------



  • 3.  RE: Re: How to convert an x-www-form-urlencoded string to JSON?

    Broadcom Employee
    Posted Dec 16, 2019 01:41 PM
    That's a really clever solution, Ryan. Out of the box there is no assertion to just do this, and I would have probably used a split on the string then looped through it to build it up, but your regex solution is waaaay more elegant. Thanks!

    ------------------------------
    Jay MacDonald - Adoption Architect - Broadcom API Management (Layer 7)
    ------------------------------



  • 4.  RE: Re: How to convert an x-www-form-urlencoded string to JSON?

    Posted Dec 16, 2019 03:23 PM
    Thanks for the quick response. It works.

    Regards,
    Prem.