npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

untruncate-json

v0.0.1

Published

Fix up the end of a partial JSON string to create valid JSON.

Downloads

34,574

Readme

untruncate-json

Fix up the end of a partial JSON string to create valid JSON.

Build Status

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, then truncateJson will assume that it was the start of the token true.
  • 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