-1

When I run the JSON.parse or decodeURI directly with the variable (_) / regex expression that gives the string, it throws error or does not decode but when I run these functions with the same string in the console, it works. I'm sure I'm missing something stupid but couldn't figure out what.

This is from the debugger console on VS code:

enter image description here

enter image description here

_ comes by parsing some values using regular expression from a html string. data is a HTML string that comes from external system. Something like:

const _ = /(?<=customer_data\['some_property'\] = ').*?(?=';)/.exec(data)?.[0] || '{}';

I can't figure out even how to approach this issue.

Kiran
  • 340
  • 2
  • 11
  • 1
    `JSON.parse()` and `decodeURI()` are *wildly* different. – Pointy Aug 27 '23 at 14:58
  • I agree but in this use case none of these are working. – Kiran Aug 27 '23 at 14:59
  • "the variable / regex expression that gives the string" — what does that mean? *What* variable? *What* regex expression? This question does not make any sense. – Pointy Aug 27 '23 at 15:01
  • What's an example of `data`? – zouabi Aug 27 '23 at 15:01
  • `data` is a html string coming from external system. I've updated the description mentioning this. – Kiran Aug 27 '23 at 15:03
  • @Pointy, in this case `variable` is `_` that's logged in the top of the image and the regular expression is `/(?<=customer_data\['some_property'\] = ').*?(?=';)/.exec(data)?.[0]` which yeilds `_`, the variable – Kiran Aug 27 '23 at 15:04
  • Is your string a literal '\x7b` etc? Because that won't work with JSON.parse. You just have a string that starts with `x` then, not actually an encoded `{`. – VLAZ Aug 27 '23 at 15:04
  • @VLAZ, yes, the string is the one logged in the image. If you see, JSON.parse() does work when I pass that string to it, at the end of the image – Kiran Aug 27 '23 at 15:07
  • 1
    You are ***NOT*** passing the same string. Try logging `_` and what you pass in the end. The string *literal* `"\x7b"` has *content* of `{` and that's what you'd see when you log it. To see the output `\x7b` then that means that's the *content* of the string, and a literal to construct this content is `"\\x7b"` which, if you log, you'd see does not show `{`. Therefore the content in `_` has to be *decoded* before you use it. Which you've shown you do, yet you don't use the output in `JSON.parse` – VLAZ Aug 27 '23 at 15:10
  • @VLAZ `JSON.parse(decodeURIComponent(_))` yeilds the same result if that's what you meant by "yet you don't use the output in `JSON.parse`". I've added the screenshot in the description – Kiran Aug 27 '23 at 15:14
  • 1
    Could you try `JSON.parse(decodeURIComponent(_.replaceAll('\\x', '%')))` (or `replace(/\\x/g, '%')`)? – zouabi Aug 27 '23 at 15:15
  • 1
    what's the output of `decodeURIComponent(_.replace(/\\x/g, '%'))`? – zouabi Aug 27 '23 at 15:19
  • Sorry, I tried `replace` and not `replaceAll`, `replaceAll` seems to work. Let me confirm with the full flow that it works – Kiran Aug 27 '23 at 15:21
  • @Kiran yeah my bad I added `replace` without the `g` option at first, then edited it to say `replaceAll` or `replace(/.../g, ...)` – zouabi Aug 27 '23 at 15:23
  • 1
    @zouabi, if you'd like to add it as an answer, I can mark it as accepted. Really appreciate the help – Kiran Aug 27 '23 at 15:23

1 Answers1

1

The string you are trying to parse is already escaped, i.e: \\x7b\\x222\\x22:\\x22041169082\\x22\\x7d.

Replace all \\xs with a % and decode it using decodeURIComponent:

const _ = '\\x7b\\x222\\x22:\\x22041169082\\x22\\x7d';

console.log(
  decodeURIComponent(_.replace(/\\x/g, '%'))
);
zouabi
  • 649
  • 1
  • 7
  • 16