GithubHelp home page GithubHelp logo

junekelly / python-datapath Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jon-betts/python-datapath

1.0 1.0 0.0 14 KB

Get and set values in deeply nested structures using JSONPath like syntax

Python 100.00%

python-datapath's Introduction

python-datapath

Use JSONPath like strings to get and set values in a deeply nested data structure with graceful retrieval if the structure does not exist and autovivification when setting values.

Inspired by, but not completely compliant with the [JSONPath specification] (http://goessner.net/articles/JsonPath/)

Why should I use this library?

YOU REALLY SHOULDN'T - This is nowhere near ready to be used yet!

datapath lets you:

  • Get values from deeply nested structures without caring if any of the intermediate keys exist
  • Set values into data structures and have all intermediate values created for you
  • Turn deeply nested structures into flat dicts and back

Compact paths

The datapath library supports compact paths which cut a small amount of the verboseness of the full JSONPath spec. A number of simplifying assumptions are made:

  • All paths are local
  • 'Naked' paths without a prefix are assumed to be dict keys
  • Bracked paths are assumed to be on the local object

Examples:

JSONPath datapath equivalents
$.a a, .a, ["a"], ['a']
$.[7] [7]
$.* , .
$..a ..a
@..a ..a
@.a a, .a, ["a"], ['a']

The JSONPath spec suggest '@.' for local anchoring and '$.' for root anchoring. Where anchoring is not specified or relevant datapath allows the omission of the leading identifiers for example:

JSONPath datapath compact
$.a, a
@.a a
$.['a'] a
@.['a'] a
$.["a"] a
@.["a"] a

NOTE! - Currently datapath doesn't allow anchoring markers

Escaping

You can set a key with a dot, or any other reserved character by escaping it:

from datapath.crud import get_path
 
print get_path('a\\.b', {'a.b' : 5})  # 5!

Compliance levels

The datapath library does not support the following parts of JSONPath at the moment:

  • List slices
  • Selectors beyond '*' - no ability to do things like book[author='lily']
  • Anchoring specifications: '@.', '$.'

Enough talking!

from datapath.crud import get_path, find_path, set_path

data = {
    'a': ['hello', 'world'],
    'b': 5
}
    
print get_path(data, 'a')     # ['hello', 'world']
print get_path(data, 'a[0]')  # 'hello'

find_path(data, '*.*')      # [['hello', 'world'], 5]

set_path({}, 'not_there[4].more', 'value')
# {'not_there': [None, None, None, {'more': 'value'}]}

Known issues

  • This library doesn't work, and using it is a fools errand
  • Attempting to set recursive paths (e.g. 'a..b') doesn't work
  • Recursive paths in general are likely to have undefined behavior

python-datapath's People

Stargazers

June Kelly avatar

Watchers

June Kelly avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.