untruncate-json
v0.0.1
Published
Fix up the end of a partial JSON string to create valid JSON.
Downloads
34,574
Maintainers
Readme
untruncate-json
Fix up the end of a partial JSON string to create valid JSON.
Motivation
Have you ever been given a string that started off as valid JSON, but was then truncated to fit under some maximum length? And even though it was invalid JSON because it was cut off in the middle, you still wanted to parse it as JSON anyways so you could extract what information you could from it?
No?
Well if you ever do, then this is the library for you!
Installation
With NPM:
npm install untruncate-json
With Yarn:
yarn add untruncate-json
Usage
Import the untruncateJson
function:
import untruncateJson from "untruncate-json";
Call it on a truncated JSON string to get a complete, valid JSON string:
untruncateJson("[1, 2"); // -> "[1, 2]"
untruncateJson('"Hello, Wor'); // -> '"Hello, Wor"'
untruncateJson('{"votes": [true, fa'); // -> '{"votes": [true, false]}'
untruncateJson("123."); // -> "123.0"
untruncateJson
will sometimes remove characters as well, if there was no way
to infer what value they were starting:
untruncateJson("[1, 2, "); // -> "[1, 2]"
untruncateJson('"abc\\'); // -> '"abc"'
untruncateJson('{"x": 20, "y": '); // -> '{"x": 20}'
Check the test cases for many more examples.
Guarantees
- As long as the input is the prefix of a valid JSON string and is not entirely whitespace, the output will be valid JSON.
- This takes into account all parts of the JSON spec, including rules around exponents in numbers, Unicode escape sequences in strings, and so forth.
untruncateJson
does not attempt to validate the input JSON. Therefore if the input JSON is not the prefix of valid JSON, then the output will not be valid JSON and will fail if you try to parse it.- The output will always contain as many correct values as can be determined
under the assumption that the input was indeed the prefix of valid JSON. For
example, if the input is cut off after a bare
t
, thentruncateJson
will assume that it was the start of the tokentrue
. - The last value appearing in the output JSON may have an incorrect value, for example if it is a truncated number or string. However, it will never have the incorrect type, e.g. if the input contained a string in that position, so will the output.
- In cases where the type of the last value cannot be inferred, then characters are removed from the end of the input as needed to avoid making a potentially incorrect guess. For example, if the truncation occurs after an object key but before any part of its corresponding value, then the entire key will be removed.
Copyright © 2019 David Philipson