Giter VIP home page Giter VIP logo

json-path's Introduction

JSON Path

Find the path of a key / value in a JSON hierarchy easily.

Motivation

When working with big and nested JSON files, sometimes it's very difficult to figure out the path of a key. You open the JSON file in a text editor, find the key / value pair you need, but then it can be a pain to get the full path of the key.

Example

Consider the following JSON file:

{
    "a": 1,
    "b": {
        "c": 2,
        "friends": [
            {
                "best": "Alice"
            },
            {
                "second": "Bob"
            },
            [5, 6, 7],
            [
                {"one": 1},
                {"two": 2}
            ]
        ]
    }
}

JSON Path will traverse it recursively and print every path / value pair:

$ ./jsonpath.py samples/short.json
root['a'] => 1
root['b']['c'] => 2
root['b']['friends'][0]['best'] => 'Alice'
root['b']['friends'][1]['second'] => 'Bob'
root['b']['friends'][2][0] => 5
root['b']['friends'][2][1] => 6
root['b']['friends'][2][2] => 7
root['b']['friends'][3][0]['one'] => 1
root['b']['friends'][3][1]['two'] => 2

The idea is to combine its usage with the Unix command grep. For instance, what's the path of the key that leads to Bob?

$ ./jsonpath.py samples/short.json | grep -i bob
root['b']['friends'][1]['second'] => 'Bob'

Then simply paste it in your application:

>>> d
{'a': 1, 'b': {'c': 2, 'friends': [{'best': 'Alice'}, {'second': 'Bob'}, [5, 6, 7], [{'one': 1}, {'two': 2}]]}}
>>> d['b']['friends'][1]['second']
'Bob'
>>>

Installation

The program was tested under Linux. It only uses the standard library, so you only need to create a virtual environment if you need a development version. With the development version you can do type checking and you can create a standalone executable.

Install the development version:

$ poetry install

Create an EXE

If you want a standalone executable, then issue the command

$ pynt exe

which will create the executable file jsonpath in the dist/ folder. (Note: for this to work, install the dev. version.)

Related Works

json-path's People

Contributors

farishijazi avatar hxuaj avatar jabbalaci avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  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.