Skip to main content

JSONxs is a Python library that uses a path expression string to get and set values in JSON and Python datastructures. It’s slightly similar to JSONPath, but supports only simpler expressions and allows modifications.

JSONxs is safe to use with untrusted input.

Examples

Given the folowing datastructure:

d = {
  'feed': {
    'id': 'my_feed',
    'url': 'http://example.com/feed.rss',
    'tags': ['devel', 'example', 'python'],
    'short.desc': 'A feed',
  }
}

We can get, set and delete values from the stucture.

jsonxs(d, 'feed.id')                                  # Get the 'id' field: 'my_feed'
jsonxs(d, 'feed.tags')                                # Get the list of tags.
jsonxs(d, 'feed.tags[-1]')                            # Get the last item in the 'tags' list: 'python'
jsonxs(d, 'feed.short\.desc')                         # Escape special char
jsonxs(d, 'feed.long\.desc', default='N/A')           # Return default when key not found
jsonxs(d, 'feed.id', ACTION_SET, 'your_feed')         # Set feed id to 'your_feed'
jsonxs(d, 'feed.tags[-1]', ACTION_SET, 'javascript')  # Replace 'python' with 'javascript'
jsonxs(d, 'feed.tags[0]', ACTION_DEL)                 # Delete the first tag
jsonxs(d, 'feed.url', ACTION_DEL)                     # Delete the 'url' key from a dict
jsonxs(d, 'feed.tags', ACTION_APPEND, 'programming')  # Append 'programming' to tags
jsonxs(d, 'feed.tags[1]', ACTION_INSERT, 'tech')      # Insert 'tech' at pos 1 in tags
jsonxs(d, 'feed.author', ACTION_SET, 'Ferry Boender') # Create new key/string value in dict
jsonxs(d, 'feed.details', ACTION_MKDICT)              # Create new key/dict value in dict
jsonxs(d, 'feed.details', ACTION_MKLIST)              # Create new key/list value in dict

Installation and usage

To install:

pip install jsonxs

To use:

from jsonxs import *
data = {'foo': 1}
jsonxs(data, 'bar', ACTION_SET, 2)
print data  # {'foo': 1, 'bar': 2}

License

JSONxs is licensed under the MIT license:

Copyright (c) 2015 Ferry Boender (ferry.boender@gmail.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for jsonxs 0.3

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

Source distribution (sdist)

Source distribution for jsonxs 0.3
File Size Uploaded
jsonxs-0.3.tar.gz 3.9 kB Details

Release files / jsonxs-0.3.tar.gz

Download URL jsonxs-0.3.tar.gz
Size 3.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2463bad387a675c62a66191164ce18f950d643c452ae17a98275fc622dcb5e26
BLAKE2b-256 checksum
How to use checksums
ff1c54c1648ef15a9bfec2390e38ac95bb7da7b0cce938db41aeb81dbc659c35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.6

1 release file

0.5

1 release file

0.4

1 release file

This release

0.3 This release

1 release file

0.2

1 release file

0.1

1 release file

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