Skip to main content

Search and select bits out of a JSON document.

Project description

jsongrep is a shell tool for extracting values from JSON documents. It supports shell-like globbing for property names, and emits the matched values separated by newlines.

Examples

Let’s start with a real-world example.

Let’s grab the even tweets from the last 10 pulled from Twitter’s JSON feed http://apiwiki.twitter.com/Twitter-REST-API-Method:-statuses-public_timeline

$ curl -s 'http://twitter.com/statuses/public_timeline.json' | jsongrep '[02468].text'
ARGHHHHHH. facebook is being gay
5-5 in the darts between Barney and Whitlock. Amazing. #darts
I wonder if I'm still located on 5th ave?
Estou de volta  a internet .... Essa chuva n para !   Estou de boa com a minha familia .

Yeah, that’s just about what I expected.

Now suppose you have a JSON document like the one in tests/ongz.json which looks like this:

{
    "bah" : {
        "feh" : true,
        "foo" : 3,
        "lah" : "songz"
    },
    "blah" : {
        "lol" : "gongz"
    },
    "arr" : [
        "a", "b", "c", "d", "e",
        "f", "g", "h", "i", "j",
        "k", "l", "m", "n", "o",
        "p", "q", "r", "s", "t",
        "u", "v", "w", "x", "y",
        "z"
    ]
}

jsongrep will let you match structural patterns, where . (dot) separates nested properties.

Let’s glob on property names:

$ jsongrep 'b*.l*' tests/ongz.json
gongz
songz

Works on arrays, too:

$ jsongrep 'arr.?' tests/ongz.json
a
b
c
d
e
f
g
h
i
j

Note that we’re still globbing, even though arrays have numeric indices:

$ jsongrep 'arr.2?' tests/ongz.json
u
v
w
x
y
z

If you specify a JSON subtree, that’s what you get back:

$ jsongrep 'bah' tests/ongz.json
{"foo": 3, "lah": "songz", "feh": true}

Syntax

jsongrep currently supports normal shell glob patterns within property names:

?       Matches any one character
*       Matches any number of characters within a field
[seq]   Matches any of the characters in seq
[!seq]  Matches any of the characters not in seq

Dot is the field separator.

Usage

Usage: jsongrep [options] [PATTERN | -e PATTERN [-e PATTERN ...]] [FILE]

Parses JSON data structurally to select a subset of data.

Options:
  --version             show program's version number and exit
  -h, --help            show this help message and exit
  -e PATTERNS, --pattern=PATTERNS
                        Additional patterns to match.
  -D, --detect-encoding
                        Attempts to detect the character encoding of input if
                        LC_TYPE and file.encoding provide insufficient hints.
                        (Slow) [default: False]

TODO

  • Support star-star (**) non-greedy matches of spanning subgraphs

  • Support unicode, escapes in patterns

  • Support extended regexps

  • Support no-pattern-matching lookup

  • Options:
    • Property separator in patterns (. by default)

    • Output separator (newline by default)

    • Quote string values in output?

    • 1/0 vs true/false for bool values in output?

Feedback

Open a ticket at http://github.com/dsc/jsongrep , or send me an email at dsc@less.ly .

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

jsongrep-0.1.1.zip (17.8 kB view details)

Uploaded Source

jsongrep-0.1.1.tar.gz (10.9 kB view details)

Uploaded Source

File details

Details for the file jsongrep-0.1.1.zip.

File metadata

  • Download URL: jsongrep-0.1.1.zip
  • Upload date:
  • Size: 17.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jsongrep-0.1.1.zip
Algorithm Hash digest
SHA256 5a2c7eb4c4f85aea5cb1ab3963747264d2262abdf9b9454d7eb377a7d2b377ce
MD5 86b84ee5b0fefcbf182831fa42ce12e1
BLAKE2b-256 bd7bdf1163a4b56ecc698b118c4e359b9fc773d69628067eba188b6fb281d575

See more details on using hashes here.

File details

Details for the file jsongrep-0.1.1.tar.gz.

File metadata

  • Download URL: jsongrep-0.1.1.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for jsongrep-0.1.1.tar.gz
Algorithm Hash digest
SHA256 8412de2a69788c28f4fa6f0310a885563b328925ea21b66baffb78a866be4521
MD5 6d77fe5a1f5388fd96c2fcb7581fe968
BLAKE2b-256 901400913ddcd459b8d4e51e881608b708c6bb7abcb9ee9362cb15b9b80024df

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