Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

json-autocomplete

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-autocomplete

Autocomplete any prefix substring of a JSON to become valid

  • 0.2.5
  • PyPI
  • Socket score

Maintainers
1

JSON Autocomplete

This library offers a simple function to complete any prefix of a valid JSON string, in a way that makes it a valid JSON again (e.g. by closing all open brackets and quotes), in a minimal way.

There is only one function, json_autocomplete(json_prefix: str) -> str, which takes a prefix of a valid JSON string and returns a valid JSON string that is the shortest possible completion of the prefix.

The heck did I develop this for? When streaming a response from a LLM like ChatGPT, where the model generates a JSON string, you can render it before the model is done generating the response.

Another use case could be when you want to allow the user to enter a JSON string, but you want to offer autocomplete suggestions. You can use this function to get the shortest possible completion of the prefix the user has entered, and then offer that as a suggestion.

Examples

>>> json_autocomplete('')
'null'
>>> json_autocomplete('n')
'null'
>>> json_autocomplete('tr')
'true'
>>> json_autocomplete('-')
'-0'
>>> json_autocomplete('2.')
'2.0'
>>> json_autocomplete('[')
'[]'
>>> json_autocomplete('{')
'{}'
>>> json_autocomplete('{"')
'{"": null}'
>>> json_autocomplete('{"a": 1, "b": 2')
'{"a": 1, "b": 2}'

Installation

pip install json-autocomplete

Then, simply import the function:

from json_autocomplete import json_autocomplete

json_autocomplete('{"a": 1, "b": 2')

Development

After making any changes to either Cython files or C++ files, you must cythonize the files:

cythonize -i json_autocomplete/*.pyx

This will 1) compile the Cython files to C++ files, and 2) compile the C++ files to shared libraries.

Afterwards, you can install the package:

pip install .

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc