Skip to content

Getting Started

This guide walks you through installing pyjsonparser and getting started with parsing and serializing JSON data.


Requirements

  • Python: >= 3.12
  • Runtime Dependencies: None (Standard Library only)

Installation

Install the latest release from PyPI:

pip install pyjsonparser

Method 2: Using Poetry

If you are using Poetry in your project:

poetry add pyjsonparser

Method 3: From Source

Clone the GitHub repository to work with the latest development code:

git clone https://github.com/alexprodan99/pyjsonparser.git
cd pyjsonparser/pyjsonparser
poetry install

Verification

Verify your installation by running a quick inline Python test:

python -c "import pyjson; p = pyjson.JsonParser(); print(p.from_string('{\"status\": \"ready\"}'))"

Output:

{'status': 'ready'}


Basic Usage

Importing the Parser

The primary entry point of the library is JsonParser, exposed directly from the pyjson package:

from pyjson import JsonParser

parser = JsonParser()

Parsing JSON Strings (from_string)

Pass any valid JSON object string to from_string():

from pyjson import JsonParser

parser = JsonParser()

# Parsing primitive types within an object
data = parser.from_string('''
{
    "title": "Parser Guide",
    "version": 1,
    "rating": 4.95,
    "published": true,
    "metadata": null,
    "tags": ["parser", "python", "json"]
}
''')

print(type(data))          # <class 'dict'>
print(data["title"])       # "Parser Guide"
print(data["version"])     # 1 (int)
print(data["rating"])      # 4.95 (float)
print(data["published"])   # True (bool)
print(data["metadata"])    # None
print(data["tags"])        # ['parser', 'python', 'json']

Serializing to JSON Strings (to_string)

Convert Python data structures into a JSON-compliant string:

from pyjson import JsonParser

parser = JsonParser()

user_dict = {
    "username": "johndoe",
    "age": 28,
    "active": True,
    "roles": ["admin", "developer"],
    "preferences": None
}

json_output = parser.to_string(user_dict)
print(json_output)
# {"username": "johndoe", "age": 28, "active": true, "roles": ["admin", "developer"], "preferences": null}

Next Steps