Skip to main content

jsonobjectify

Development Status PyPI version Python versions

CI

A lightweight Python library for converting JSON data into Python objects, simplifying access to nested structures through dot notation. Instead of writing chained dictionary lookups, you can navigate nested JSON structures using a clean, readable, object-oriented interface.

Installation

pip install jsonobjectify

Usage

Consider the following JSON string/response:

response = """{
    "id": 1,
    "name": "John Doe",
    "email": "john.doe@example.com",
    "website": "www.example.dev",
    "address": {
      "city": "Austin",
      "state": "TX",
      "country": "United States",
      "geo_location": {"lat": "30.2672", "lng": "-97.7431"}
    },
    "skills": [
      {"name": "Python", "level": "Advanced"},
      {"name": "C", "level": "Advanced"},
      {"name": "C++", "level": "Intermediate"}
    ]
}"""

Construct python object from JSON string/response

>>> from jsonobjectify import JsonPath
>>> employee = JsonPath.from_string(response)

Attribute look-up

You can now access and traverse nested JSON objects using dot notation. For JSON arrays, use the standard Python indexing syntax.

>>> employee.name
'John Doe'
>>> employee.address.city
'Austin'
>>> employee.address.geo_location.lng
'-97.7431'
>>> employee.skills[0].name
'Python'
>>> employee.skills[1].level
'Advanced'
>>>

You can also iterate over JSON arrays using a standard Python for loop and access each element's properties using dot notation.

>>> for skill in employee.skills:
...     print(f"{skill.name}, {skill.level}")
...     
Python, Advanced
C, Advanced
C++, Intermediate
>>> 

Attribute Assignment

>>> employee.name = "Jane Doe"
>>> employee.address.city = "Fort Worth"

Array of JSON objects

A JSON response can contain an array of objects at the root level. You can use JsonPath.from_string to parse the response and access each object's properties using dot notation.

response = """[
  {
    "name": "John Doe",
    "email": "john.doe@example.com",
    "website": "www.example.dev"
  },
  {
    "name": "Foo Bar",
    "email": "foo.bar@example.com",
    "website": "www.example.io"
  },
  {
    "name": "Baz Qux",
    "email": "baz.qux@example.com",
    "website": "www.example.edu"
  }
]"""
>>> employees = JsonPath.from_string(response)
>>> len(employees)
3

Indexing each employee

>>> employees[0]
<jsonobjectify.jsonobject.JsonObject object at 0x101bb7d50>
>>> employees[1]
<jsonobjectify.jsonobject.JsonObject object at 0x101bb7b50>
>>> employees[2]
<jsonobjectify.jsonobject.JsonObject object at 0x101aa2120>
>>> employees[0].name
'John Doe'
>>> employees[0].email
'john.doe@example.com'
>>> employees[0].website
'www.example.dev'
>>> 

Iterating over JSON arrays using a standard Python for loop and access each element's properties using dot notation.

>>> for employee in employees:
...     print(f"{employee.name}, {employee.email}, {employee.website}")
...     
John Doe, john.doe@example.com, www.example.dev
Foo Bar, foo.bar@example.com, www.example.io
Baz Qux, baz.qux@example.com, www.example.edu
>>> 

JsonPath object from a JSON file

Create a JsonPath object directly from a JSON file using the from_file class method. This allows you to work with JSON data stored in a file without having to read and parse its contents manually.

For example, consider an employee.json file containing the employee data shown in the previous example.

>>> employee = JsonPath.from_file("employee.json")

Once the object is created, you can access nested properties and iterate over JSON arrays in the same way

>>> employee.name
'John Doe'
>>> employee.address.city
'Austin'
>>> for skill in employee.skills:
...     print(f"{skill.name}, {skill.level}")
...
Python, Advanced
C, Advanced
C++, Intermediate

Working with Scalar Values

If the root JSON value is a scalar, such as a string, number, boolean or null, you can access the underlying value using the value property.

>>> data = JsonPath.from_string("26")
>>> data.value
26
>>> data = JsonPath.from_string("true")
>>> data.value
True
>>> data = JsonPath.from_string("null")
>>> data.value
>>>
>>> json = JsonPath.from_string('"hello world"')
>>> json.value
'hello world'

Accessing JSON Properties with Normalized Keys

JSON property names may contain hyphens, spaces, or periods, which cannot be used directly in Python attribute names. jsonobjectify normalizes these characters to underscores, allowing you to access such properties using dot notation.

response = """{
  "first-name": "Alice",
  "job title": "Software Engineer",
  "contact.email": "alice@example.com"
}"""
>>> employee = JsonPath.from_string(response)

You can access these properties using normalized attribute names,

>>> employee.first_name
'Alice'
>>> employee.job_title
'Software Engineer'
>>> employee.contact_email
'alice@example.com'

The original JSON property names are preserved when converting the object back to a dictionary or JSON string.

Converting Objects Back to JSON

jsonobjectify is intended to support conversion back to standard Python dictionaries and JSON strings, making it convenient to work with transformed data.

# Convert the object to a Python dictionary
data = employee.to_dict()

# Serialize the object to a JSON string
json_data = employee.to_json()

Requirements

  • Python 3.12 or later

Contributing

Contributions, bug reports, and feature suggestions are welcome. Please open an issue to discuss a proposed change before submitting a pull request.

License

This package is distributed under the terms of MIT license

Metadata

Release files for jsonobjectify 0.3.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for jsonobjectify 0.3.1
File Size Uploaded Provenance
jsonobjectify-0.3.1.tar.gz 8.3 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for jsonobjectify 0.3.1
File Interpreter ABI Platform
jsonobjectify-0.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 16.5 kB

Release files / jsonobjectify-0.3.1.tar.gz

Download URL jsonobjectify-0.3.1.tar.gz
Size 8.3 kB
Tags Source
SHA-256 checksum
How to use checksums
6fe061d55c7ab9a6a2b7578e1dbc26a4c0132d679ece025e13370f4f66bab5e4
BLAKE2b-256 checksum
How to use checksums
4061b41581724ea089c63115a19fc8908950dc6625a02bd7f1b9c0a16f8daab0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Release files / jsonobjectify-0.3.1-py3-none-any.whl

Download URL jsonobjectify-0.3.1-py3-none-any.whl
Size 8.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
138449ae9f9e3a3c01e746f4f4782f2e3abb5a28dfc87d64efe689df18e5b7fd
BLAKE2b-256 checksum
How to use checksums
c5c22c47f52cf749d96b63e4adb5a9515aa7729a99581b7da865821edd01cb76
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.14.8

Security for jsonobjectify 0.3.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page