Skip to main content

Truncated JSON completer

Project description

truncjson

A library to complete truncated JSON strings.

Install

pip install truncjson

Use Cases

  1. When you possess a JSON file that is truncated, and you wish to parse its current content.
  2. When you are receiving JSON content as a stream from services like ChatGPT. This allows you to parse the JSON content each time you receive a new chunk.

Simple Usage

from truncjson import complete

result = complete('{"k1":"v1","k2":20.5,"k3":[12')
print(result)

# Output: {"k1":"v1","k2":20.5,"k3":[12]}

Stream Usage

Here we created a dummy stream with a list of chunks.

from truncjson import Stream

chunks = [
    'Here yo',
    'u go {"k1":"v1","k2":2',
    '0.5,"k3":[12,34,"56",tr',
    'ue,{"k31":"v31","k32":false,"k33":n',
    'ull}]}\nAnd ano',
    'ther one {"k',
    'ey":1.',
    '12} {"a',
    'b":"cd"}'
]

stream = Stream()
for chunk in chunks:
    print(stream.extract(chunk))

# Output:
# []
# ['{"k1":"v1","k2":2}']
# ['{"k1":"v1","k2":20.5,"k3":[12,34,"56",true]}']
# ['{"k1":"v1","k2":20.5,"k3":[12,34,"56",true,{"k31":"v31","k32":false,"k33":null}]}']
# ['{"k1":"v1","k2":20.5,"k3":[12,34,"56",true,{"k31":"v31","k32":false,"k33":null}]}']
# ['{"k1":"v1","k2":20.5,"k3":[12,34,"56",true,{"k31":"v31","k32":false,"k33":null}]}', '{"k":null}']
# ['{"k1":"v1","k2":20.5,"k3":[12,34,"56",true,{"k31":"v31","k32":false,"k33":null}]}', '{"key":1.0}']
# ['{"k1":"v1","k2":20.5,"k3":[12,34,"56",true,{"k31":"v31","k32":false,"k33":null}]}', '{"key":1.12}', '{"a":null}']
# ['{"k1":"v1","k2":20.5,"k3":[12,34,"56",true,{"k31":"v31","k32":false,"k33":null}]}', '{"key":1.12}', '{"ab":"cd"}']

ChatGPT Usage

import openai
import json
from truncjson import Stream

openai.api_key = '###'

chat = openai.ChatCompletion.create(
    model='gpt-3.5-turbo',
    messages=[
        {'role': 'system', 'content': 'You are a intelligent assistant.'},
        {'role': 'user', 'content': 'Return 3 different random JSON objects each has more than 500 characters.'},
    ],
    stream=True
)

stream = Stream()
for chunk_obj in chat:
    chunk_content = chunk_obj['choices'][0]['delta'].get('content', '')
    for obj in stream.extract(chunk_content):
        print(json.loads(obj))

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

truncjson-1.2.12.tar.gz (132.3 kB view hashes)

Uploaded Source

Built Distributions

truncjson-1.2.12-cp312-cp312-win_amd64.whl (57.1 kB view hashes)

Uploaded CPython 3.12 Windows x86-64

truncjson-1.2.12-cp312-cp312-win32.whl (50.9 kB view hashes)

Uploaded CPython 3.12 Windows x86

truncjson-1.2.12-cp312-cp312-musllinux_1_1_x86_64.whl (348.8 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ x86-64

truncjson-1.2.12-cp312-cp312-musllinux_1_1_i686.whl (331.3 kB view hashes)

Uploaded CPython 3.12 musllinux: musl 1.1+ i686

truncjson-1.2.12-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (349.3 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

truncjson-1.2.12-cp312-cp312-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (328.8 kB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

truncjson-1.2.12-cp312-cp312-macosx_10_9_x86_64.whl (58.8 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ x86-64

truncjson-1.2.12-cp312-cp312-macosx_10_9_universal2.whl (113.2 kB view hashes)

Uploaded CPython 3.12 macOS 10.9+ universal2 (ARM64, x86-64)

truncjson-1.2.12-cp311-cp311-win_amd64.whl (57.0 kB view hashes)

Uploaded CPython 3.11 Windows x86-64

truncjson-1.2.12-cp311-cp311-win32.whl (50.9 kB view hashes)

Uploaded CPython 3.11 Windows x86

truncjson-1.2.12-cp311-cp311-musllinux_1_1_x86_64.whl (335.3 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ x86-64

truncjson-1.2.12-cp311-cp311-musllinux_1_1_i686.whl (320.6 kB view hashes)

Uploaded CPython 3.11 musllinux: musl 1.1+ i686

truncjson-1.2.12-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (334.7 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

truncjson-1.2.12-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (319.2 kB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

truncjson-1.2.12-cp311-cp311-macosx_10_9_x86_64.whl (58.5 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ x86-64

truncjson-1.2.12-cp311-cp311-macosx_10_9_universal2.whl (113.1 kB view hashes)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

truncjson-1.2.12-cp310-cp310-win_amd64.whl (56.8 kB view hashes)

Uploaded CPython 3.10 Windows x86-64

truncjson-1.2.12-cp310-cp310-win32.whl (51.1 kB view hashes)

Uploaded CPython 3.10 Windows x86

truncjson-1.2.12-cp310-cp310-musllinux_1_1_x86_64.whl (312.5 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ x86-64

truncjson-1.2.12-cp310-cp310-musllinux_1_1_i686.whl (298.9 kB view hashes)

Uploaded CPython 3.10 musllinux: musl 1.1+ i686

truncjson-1.2.12-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (307.4 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

truncjson-1.2.12-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (294.7 kB view hashes)

Uploaded CPython 3.10 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

truncjson-1.2.12-cp310-cp310-macosx_10_9_x86_64.whl (58.4 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ x86-64

truncjson-1.2.12-cp310-cp310-macosx_10_9_universal2.whl (113.0 kB view hashes)

Uploaded CPython 3.10 macOS 10.9+ universal2 (ARM64, x86-64)

truncjson-1.2.12-cp39-cp39-win_amd64.whl (57.7 kB view hashes)

Uploaded CPython 3.9 Windows x86-64

truncjson-1.2.12-cp39-cp39-win32.whl (52.0 kB view hashes)

Uploaded CPython 3.9 Windows x86

truncjson-1.2.12-cp39-cp39-musllinux_1_1_x86_64.whl (322.1 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ x86-64

truncjson-1.2.12-cp39-cp39-musllinux_1_1_i686.whl (308.8 kB view hashes)

Uploaded CPython 3.9 musllinux: musl 1.1+ i686

truncjson-1.2.12-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (315.5 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

truncjson-1.2.12-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (302.3 kB view hashes)

Uploaded CPython 3.9 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

truncjson-1.2.12-cp39-cp39-macosx_10_9_x86_64.whl (60.0 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ x86-64

truncjson-1.2.12-cp39-cp39-macosx_10_9_universal2.whl (116.4 kB view hashes)

Uploaded CPython 3.9 macOS 10.9+ universal2 (ARM64, x86-64)

truncjson-1.2.12-cp38-cp38-win_amd64.whl (58.0 kB view hashes)

Uploaded CPython 3.8 Windows x86-64

truncjson-1.2.12-cp38-cp38-win32.whl (52.1 kB view hashes)

Uploaded CPython 3.8 Windows x86

truncjson-1.2.12-cp38-cp38-musllinux_1_1_x86_64.whl (330.6 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ x86-64

truncjson-1.2.12-cp38-cp38-musllinux_1_1_i686.whl (318.0 kB view hashes)

Uploaded CPython 3.8 musllinux: musl 1.1+ i686

truncjson-1.2.12-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (314.5 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

truncjson-1.2.12-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (301.6 kB view hashes)

Uploaded CPython 3.8 manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

truncjson-1.2.12-cp38-cp38-macosx_10_9_x86_64.whl (60.3 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ x86-64

truncjson-1.2.12-cp38-cp38-macosx_10_9_universal2.whl (117.0 kB view hashes)

Uploaded CPython 3.8 macOS 10.9+ universal2 (ARM64, x86-64)

truncjson-1.2.12-cp37-cp37m-win_amd64.whl (58.2 kB view hashes)

Uploaded CPython 3.7m Windows x86-64

truncjson-1.2.12-cp37-cp37m-win32.whl (52.1 kB view hashes)

Uploaded CPython 3.7m Windows x86

truncjson-1.2.12-cp37-cp37m-musllinux_1_1_x86_64.whl (292.4 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ x86-64

truncjson-1.2.12-cp37-cp37m-musllinux_1_1_i686.whl (287.2 kB view hashes)

Uploaded CPython 3.7m musllinux: musl 1.1+ i686

truncjson-1.2.12-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (290.5 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

truncjson-1.2.12-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (277.6 kB view hashes)

Uploaded CPython 3.7m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

truncjson-1.2.12-cp37-cp37m-macosx_10_9_x86_64.whl (60.4 kB view hashes)

Uploaded CPython 3.7m macOS 10.9+ x86-64

truncjson-1.2.12-cp36-cp36m-win_amd64.whl (66.5 kB view hashes)

Uploaded CPython 3.6m Windows x86-64

truncjson-1.2.12-cp36-cp36m-win32.whl (56.7 kB view hashes)

Uploaded CPython 3.6m Windows x86

truncjson-1.2.12-cp36-cp36m-musllinux_1_1_x86_64.whl (275.2 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ x86-64

truncjson-1.2.12-cp36-cp36m-musllinux_1_1_i686.whl (274.0 kB view hashes)

Uploaded CPython 3.6m musllinux: musl 1.1+ i686

truncjson-1.2.12-cp36-cp36m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_17_x86_64.manylinux2014_x86_64.whl (271.6 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ x86-64 manylinux: glibc 2.5+ x86-64

truncjson-1.2.12-cp36-cp36m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl (261.2 kB view hashes)

Uploaded CPython 3.6m manylinux: glibc 2.17+ i686 manylinux: glibc 2.5+ i686

truncjson-1.2.12-cp36-cp36m-macosx_10_9_x86_64.whl (57.2 kB view hashes)

Uploaded CPython 3.6m macOS 10.9+ x86-64

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page