Skip to main content

quickly edit json files from the command line

Project description

jse - JSON Editor Build Status

quickly edit json files from the command line

jse is pragmatic and terse. It lets you edit json fast, without needing to care about quotes, types, exact indexes, or any of the stuff that makes json a pain.

Usage

$ jse <file> <mode> <key> <value>

TLDR Version

edit an existing key: -e --edit

$ jse config.json --edit app.version 0.3.3

add a new element: -a --add

$ jse todo.json --add list.shopping {task:eggs,done:false}

delete a value: -d --delete

$ jse problems.json --delete problems[99]

full examples with json files below

Installing

comming soon - pip install :)

Running from Source

requiremets:

  • python 3

steps:

  1. clone the repository
  2. make it executable chmod +x jse.py
  3. put in on the path ln -s /path/to/jse.py ~/.local/bin/jse

jse has no package dependencies (it literally just edits json), but does use pytest for tests.

Examples

lets start with a json file

# example.json
{
    "users": [
        {"name": "alice", "age": 21, "admin": false},
        {"name": "bob", "age": 57, "admin": true},
        {"name": "charlie", "age": 37, "admin": false}
    ]
}

We want to delete the user alice using jse. All we need to do is specify -d or --delete mode and the path to her user object

$ jse example.json -d users[0]

we can use both index or dot notation

$ jse example.json -d users.0
# example.json
{
    "users": [
        {"name": "bob", "age": 57, "admin": true},
        {"name": "charlie", "age": 37, "admin": false}
    ]
}

now lets make charlie an admin. To edit an existing field we use the edit command with -e or --edit. Edit takes a key to change and its new value.

$ jse example.json -e users.1.admin true
# example.json
{
    "users": [
        {"name": "bob", "age": 57, "admin": true},
        {"name": "charlie", "age": 37, "admin": true}
    ]
}

jse is smart enough to infer datatypes from the command line. it can also accept complex nested objects and arrays in a terse, quote-free format. Lets add a new nested field to the file with --add or -a

$ jse example.json -a highscore [{score:32.5,user:bob,metadata:{ip:192.168.1.102,client:firefox}}]
{
    "users": [
        {"name": "bob", "age": 57, "admin": true},
        {"name": "charlie", "age": 37, "admin": true}
    ],
    "highscore": [
        {
            "score": "32.5",
            "user": "bob",
            "metadata": {
                "ip": "192.168.1.102",
                "client": "firefox"
            }
        }
    ]
}

jse also understands lists, so we can add new elements to a one without needing an explicit index. It will infer we are trying to append from --add instead of changing the list to an object (--edit)

$ jse example.json -a highscore {"score":52,"user": "charlie"}
{
    "users": [
        {"name": "bob", "age": 57, "admin": true},
        {"name": "charlie", "age": 37, "admin": true}
    ],
    "highscore": [
        {
            "score": "32.5",
            "user": "bob",
            "metadata": {
                "ip": "192.168.1.102",
                "client": "firefox"
            }
        },
        {
            "score": 52.0,
            "user": "charlie"
        }
    ]
}

error messages are also meant to be informative, because no one wants a KeyError

$ jse example.json -a users.0.name "not bob"
'name' already has a value. Use --edit to modify it
$ jse example.json -d users[2]
There is no element with index 2. The largest index is 1

You can also delete mulitple keys using -d, by passing them seperately

$ jse example.json -d users.0.age users.1.age
{
    "users": [
        {
            "name": "bob",
            "admin": true,
        },
        {
            "name": "charlie",
            "admin": true
        }
    ]
    ...
}

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

jse-0.0.1.tar.gz (3.1 kB view details)

Uploaded Source

Built Distribution

jse-0.0.1-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file jse-0.0.1.tar.gz.

File metadata

  • Download URL: jse-0.0.1.tar.gz
  • Upload date:
  • Size: 3.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for jse-0.0.1.tar.gz
Algorithm Hash digest
SHA256 ab345d7b8218a34ae4e7144531ab0dd6ac3c5fb090780c4f04570968c4100cce
MD5 cc60a40dd6985b7ef05923aa7a1990a2
BLAKE2b-256 7fd056318e804c699a3782b084c874ded1be22d0f4ab62623542f6074c7b0797

See more details on using hashes here.

File details

Details for the file jse-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: jse-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7

File hashes

Hashes for jse-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 2413418e908c2ee0b6e1b9950e97fb770e32d2e5f55a070f39728055693b8a98
MD5 f322c679b9ed5753f166b59bad04ced4
BLAKE2b-256 06025b4517946466b719abf293de9c1736c562895286b5419359503022511116

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