Home > other >  How to fix an invalid random string to make it JSON valid
How to fix an invalid random string to make it JSON valid

Time:11-26

In Javascript, I need to "fix" a string, supposed to be JSON valid but may not be. The string has the following format (the unknown part is marked with "<INVALID_CHARS>"):

[
    { "key_1": "ok_data", "key_2": "something_valid <INVALID_CHARS>"},
    { "key_1": "ok_data", "key_2": "some_valid_value"}
]

"INVALID_CHARS" are chars which make the JSON.parse() function fail. The errors are always localized on the "key_2" property of this array elements.

Note that these chars come from random binary data, and can thus be anything.

I would like to find the simplest solution, or at least one which is the least prone to errors.

I thought of replacing invalid characters, but there is also a problem with single backslash chars followed by a non special char, throwing an error too, or quote chars. And I probably did not think of all the possible errors.

Thank you.

CodePudding user response:

JSON is not allowed to contain arbitrary binary data; it must be a sequence of valid Unicode codepoints. (Usually these are transmitted in UTF-8 encoding, but regardless, arbitrary binary data is not possible.) So if you want to include arbitrary binary data you'll need to figure out how to unambiguously encode it for transmission. If you don't encode it in some way, then you won't be able to reliably distinguish a byte which happens to have the same code as " from the " which terminates the string.

There are a number of possible encodings you might use for which standard libraries exist in most languages. One of the most commonly used is base-64.

CodePudding user response:

Have you tried replacing all "s with \"?

  • Related