-3

My JSON string looks like this

[{"id":0,"nextCallMills":0,"delay":0,"start":"...

This doesn't work in JSON.parseString()

unescape() and URIdecode() did not work. How can I convert this string so that parseString would understand it as JSON?

124697
  • 22,097
  • 68
  • 188
  • 315
  • 2
    it's not valid JSON, so you can't. It appears to have been mistakenly html-encoded (not url-encoded or escaped) before you started working with it. You should fix whatever is doing that to it, as it's corrupting the formatting. – ADyson Jan 08 '19 at 17:25
  • 1
    As this is invalid JSON I would fix the source of this invalid JSON. – Andreas Jan 08 '19 at 17:25
  • @Andreas this is only a snippet. the whole json is valid and produced by Gson – 124697 Jan 08 '19 at 17:26
  • 2
    If it's valid why would you need to fix it? O.o – Andreas Jan 08 '19 at 17:27
  • " the whole json is valid"... no, it's not. If it was valid, you wouldn't have a problem, would you? It might have been ok when you generated it, but in between times something has HTML-encoded it, as I've already stated. It's changed all the quote marks round the property names and values into HTML-encoded equivalents, which renders it invalid as JSON. – ADyson Jan 08 '19 at 17:27
  • @ADyson So a valid answer would be a way to decode it to valid json – Michiel Dral Jan 08 '19 at 17:28
  • @MichielDral maybe, but a far better solution would be to stop it being corrupted in the first place... – ADyson Jan 08 '19 at 17:29
  • @ADyson So how can we decode it back so that we can parse it to json – 124697 Jan 08 '19 at 17:29
  • 2
    @code511788465541441 you could try to do that, but it's a waste of time. Focus on fixing whatever is corrupting in in the first place. decoding it again is just papering over the cracks, and wasting CPU – ADyson Jan 08 '19 at 17:30
  • If the whole JSON is valid, and this is a snippet of that json, I assume the entire JSON file is encoded like this? If so, then *no part* of that JSON is actually valid. –  Jan 08 '19 at 17:30

2 Answers2

0

It is html encoded, which is different to being URI encoded. There is, AFAIK, no built in function to decode html entities. However, this answers provides a simple function: https://stackoverflow.com/a/43282001/2681964

copied over the code snippet from the mentioned answer

function convertHTMLEntity(text){
    const span = document.createElement('span');

    return text
    .replace(/&[#A-Za-z0-9]+;/gi, (entity,position,text)=> {
        span.innerHTML = entity;
        return span.innerText;
    });
}

console.log(JSON.parse(convertHTMLEntity(your_encoded_json)));

However, this uses the DOM so can only be used in the browser. Assuming that only the "s are encoded, and you need to run this code in a non-browser environment, you can use

console.log(JSON.parse(your_encoded_json.replace(/"/g, '"')));
Michiel Dral
  • 3,932
  • 1
  • 19
  • 21
  • this works, but as I pointed out earlier, why waste time working around a problem when it would be better to actually fix the problem at source? – ADyson Jan 08 '19 at 23:07
  • 1
    @ADyson in an ideal world we would fix every problem at the core, but a lot of time software is just built the way it is built. I've experienced a lot of problems myself where getting to the real problem was either impossible, or would have taken a lot of time. In those cases I think it is just as useful to just hack around the problem and get back to making stuff that actually has impact on the world. Not to say that those things can't be important, but the end user in this case will not notice a single bit if he decoded the string or fixed the problem at the core. – Michiel Dral Jan 08 '19 at 23:16
  • Sometimes software is built the way it is, sure, I've seen that. But in this case, Html-encoding some JSON is pretty daft, almost certainly unintentional and should be easily reversible - no JSON serialising library would create it like that, so probably some other filter on the server or client is messing with it before it arrives to where the OP is trying to use it ... A bit of debugging ought to find it fairly easily. – ADyson Jan 09 '19 at 00:44
  • 1
    @ADyson "and should be easily reversible" is an assumption I'm less temped to make. If you think it is, feel free to solve the real problem with the OP, but in the meantime I just provided a hacky, yet working answer :) – Michiel Dral Jan 09 '19 at 00:50
  • I'd love to solve it but of course as you can see there's no information about where the data comes from, so OP will need to figure it out, if they want to, or post extra information. – ADyson Jan 09 '19 at 08:19
0

Simply replace " by it's ascii value " :

JSON.parse("[{"id":0,"nextCallMills":0}]".split('"').join('"'))

Yields :

(1) […]
0: {…}
  id: 0
  nextCallMills: 0
  ...
Loïc
  • 11,804
  • 1
  • 31
  • 49