0

So I have this main URL

myapp://page/open-slideup-menu?params=${encodeURIComponent(JSON.stringify(this._getDeeplinkParams()))}

The _getDeeplinkParams function returns and nested JSON object like

{
      header_text: 'Post Options',
      cancel_text: 'Cancel',
      options: optionsArray
}

The optionsArray is as array that has nested JSON that can contain URL in them as well ..

    const deletePostAction =  {
      url: `https://api.com/posts/XXX`,
      method: 'DELETE'
    };

    const deletePostOption = {
      text: 'Delete',
      action: {
        url: `myapp://page/delete-item?id=XXX&action=${JSON.stringify(deletePostAction)}`,
      }
    };

    optionsArray  = [deletePostOption];

As you can see the options array has a nested JSON with URL inside of it.. I need to stringify the URL inside the deletePostOption..

But it stringifies it twice and produces an unparsable main deeplink..

myapp://page/open-slideup-menu?params={"header_text":"Post Options","cancel_text":"Cancel","options":[{"text":"Delete","action":{"url":"myapp://page/delete-item?id=XXX&action={\"url\":\"https://api.com/posts/XXX\",\"method\":\"DELETE\"}"}}]}

the nested second URL in the has \" in it .. SO that makes it inparsable..

Any clean way of doing this without .replace(/^\\"g/,'"'), so that all the urls are and deeplinks are properly encoded and it works for more nested urls

My ideal output would be something like this

myapp://page/open-slideup-menu?params={"header_text":"Post Options","cancel_text":"Cancel","options":[{"text":"Delete","action":{"url":"myapp://page/delete-item?id=XXX&action={"url":"https://api.com/posts/XXX","method":"DELETE"}"}}]}

Or if any other standard output used in production systems please let me know

Sharvil
  • 129
  • 12
  • Can you show what you have tried? – Andre Nuechter Nov 01 '19 at 18:04
  • This is my main try which produces wrong result: ```myapp://page/open-slideup-menu?params=${encodeURIComponent(JSON.stringify(this._getDeeplinkParams()))}``` – Sharvil Nov 01 '19 at 18:07
  • I tried using `.replace(/^\\"g/,'"')` after the JSON stringify.. but it seems like a hacky way of doing it so don't want to relly on that – Sharvil Nov 01 '19 at 18:07
  • Have a look at this [thread](https://stackoverflow.com/questions/16507858/json-stringify-escapes-double-quotes-every-time-when-stringified) – Andre Nuechter Nov 01 '19 at 18:28

0 Answers0