Skip to main content

Use path expression strings to get and set values in JSON

Project description

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:

import jsonxs

data = {'foo': 1}
jsonxs.jsonxs(data, 'bar', jsonxs.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.

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

jsonxs-0.6.tar.gz (4.1 kB view details)

Uploaded Source

File details

Details for the file jsonxs-0.6.tar.gz.

File metadata

  • Download URL: jsonxs-0.6.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jsonxs-0.6.tar.gz
Algorithm Hash digest
SHA256 dc41ae07961f3f19f97241e4c7f611d84d076c420dd2876aadfd936e59c8c302
MD5 35bf2ed747619e0ce5a9bd4d4f204bd6
BLAKE2b-256 656267257a84338fde1b89fce8b28164364bf2ad6a5a4459a6a890cf497cf721

See more details on using hashes here.

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