Py.Cafe

maartenbreddels/

jiter-demo

Parse JSON Data with Streamlit and Jiter

DocsPricing
  • app.py
  • jiter-0.6.1-cp311-cp311-emscripten_3_1_46_wasm32.whl
  • jiter-0.6.1-cp312-cp312-pyodide_2024_0_wasm32.whl
  • requirements.txt
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import streamlit as st
import jiter

# based on https://github.com/pydantic/jiter/tree/main/crates/jiter-python

json_data = b'{"name": "John", "age": 30}'
parsed_data = jiter.from_json(json_data)
st.write(parsed_data)  # Output: {'name': 'John', 'age': 30}

partial_json = b'{"name": "John", "age": 30, "city": "New Yor'

# Raise error on incomplete JSON
try:
    jiter.from_json(partial_json, partial_mode=False)
except ValueError as e:
    print(f"Error: {e}")

# Parse incomplete JSON, discarding incomplete last field
result = jiter.from_json(partial_json, partial_mode=True)
st.write(result)  # Output: {'name': 'John', 'age': 30}

# Parse incomplete JSON, including incomplete last field
result = jiter.from_json(partial_json, partial_mode='trailing-strings')
st.write(result)  # Output: {'name': 'John', 'age': 30, 'city': 'New Yor'}
requirements.txt
1
2
3
4
5
6

streamlit==1.27.2  # currently pinned to this version
jiter @ https://py.cafe/files/maartenbreddels/jiter-demo/jiter-0.6.1-cp312-cp312-pyodide_2024_0_wasm32.whl
# older pyodide (v0.25.1) might want to use
# https://py.cafe/files/maartenbreddels/jiter-example/jiter-0.6.1-cp311-cp311-emscripten_3_1_46_wasm32.whl
openai