Skip to content

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

License

Notifications You must be signed in to change notification settings

jabbalaci/JSON-path

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

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

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages