Code Monkey home page Code Monkey logo

jison's Introduction

Jison

Jison is a tiny all-Powerful Fluent Json parser (Python3.6)

Jison is a simple but powerful parser for Json manipulation. It parses a Json string into a dictionary with syntax check like what Python's builtin method json.loads() does, or write to file like json.dump(), but beside these it provides many additional features:

(the object refers any Json key:value pair)

  1. Fluent dictionary of Json operation with dots ., for example: json.key1.key2.key3[0].key4
  2. String search under tree structure & hierarchical search (requires a 3rd party string match function which returns int/float in [0, 1] as parameter to work)
  3. Single Json object acquisition (get one Json object returned as dict)
  4. Multiple Json object acquisition (get all Json objects under any depth with same key value and return them as a list of dictionary)
  5. Json object deletion (delete any single Json object)
  6. Json object replacement (find and replace any single Json object)

Examples

Assuming sample.json has following content:

{"id": 1,
 "params": [{"key1": "main",
             "key2": "client",
             "key3": "0/0",
             "key4": 0,
             "key5": 2},
            {"c1": null, "c2": true, "key1": "sub", "key2": "parent"}],
 "sample": "Jison"}
# the initialization can be a Json string, a Python dict or from a `sample` + `.json` file
>>> jison = Jison(fp=open('sample.json', 'r'))
# parse the Json and get dictionary
>>> json = jison.parse()
>>> json
""" result
{'id': 1,
 'params': [{'key1': 'main',
             'key2': 'client',
             'key3': '0/0',
             'key4': 0,
             'key5': 2},
            {'c1': None, 'c2': True, 'key1': 'sub', 'key2': 'parent'}],
 'sample': 'Jison'}
"""

# fluent json operation
>>> json.params[0].key1
""" result
'main'
"""

# get single Json object in dictionary
>>> jison.get_object(obj_name='params')
""" result
{'params': [{'key1': 'main',
             'key2': 'client',
             'key3': '0/0',
             'key4': 0,
             'key5': 2},
            {'c1': None, 'c2': True, 'key1': 'sub', 'key2': 'parent'}]}
"""

# get multiple Json objects
>>> jison.get_multi_object(obj_name='key1')
""" result
[{'key1': 'main'}, {'key1': 'sub'}]
"""

# get a list of multiple Json objects, or None if no key is matched
>>> key1, key99, key2 = jison.get_multi_object(obj_name=['key1', 'key99, 'key2'])
""" result
key1:  [{'key1': 'main'}, {'key1': 'sub'}]
key99: None
key2:  [{'key2': 'client'}, {'key2': 'parent'}]
"""

# delete Json object and return a Jison instance, this operation will be written to file which the Json is loaded from
>>> jison.remove_object(obj_name='params').json
""" result
{'id': 1, 'sample': 'Jison'}
"""

# replace a Json object with another and return a Jison instance, this operation will be written to file which the Json is loaded from
>>> jison.replace_object(obj_name='params', new_chunk={"replaced": "new_data"}).json
""" result
{'id': 1, 'replaced': 'new_data', 'sample': 'Jison'}
"""

jison's People

Stargazers

 avatar  avatar

Watchers

 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.