Skip to main content

jq for pythonista

Project description

jqfpy

Build Status

jq is too difficult

jq is too difficult, at least for me.

For example, extracting key-name when use is true only, from below JSON data.

{
  "apps": {
    "foo": {
      "use": true
    },
    "bar": {
      "use": true
    },
    "boo": {
      "use": true
    },
    "bee": {
      "use": false
    }
  }
}

What is jq's answer? (taking over 30 minutes, my past challenges).

$ cat data.json | jq '.apps | . as $$o | keys | map(select($$o[.].use))'
[
  "bar",
  "boo",
  "foo"
]

If you have python's knowledge, this is tiny oneliner, isn't it?

$ cat data.json | jqfpy '[k for k, opts in get("apps").items() if opts["use"]]'
[
  "foo",
  "bar",
  "boo"
]

(get() is special function, like a json.load(sys.stdin).get.)

install

$ pip install jqfpy

how to use

describe syntax

todo.

tutorial

this is jqfpy version of jq's Tutorial

$ alias jsonDATA="curl 'https://api.github.com/repos/stedolan/jq/commits?per_page=5'"
# jq.
$ jsonDATA | jq '.'
# jqfpy.
$ jsonDATA | jqfpy 'get()'
# jq.
$ jsonDATA | jq '.[0]'
# jqfpy.
$ jsonDATA | jqfpy 'get()[0]'
# jq.
$ jsonDATA | jq '.[0] | {message: .commit.message, name: .commit.committer.name}'
# jqfpy.
$ jsonDATA | jqfpy 'd = get()[0]; {"message": get("commit/message", d), "name": get("commit/committer/name", d)}'
# or
$ jsonDATA | jqfpy '{"message": get("0/commit/message"), "name": get("0/commit/committer/name")}'
# jq.
$ jsonDATA | jq '.[] | {message: .commit.message, name: .commit.committer.name}'
# jqfpy.
$ jsonDATA | jqfpy --squash 'L = get(); [{"message": get("commit/message", d), "name": get("commit/committer/name", d)} for d in L]'
# jq.
$ jsonDATA | jq '[.[] | {message: .commit.message, name: .commit.committer.name, parents: [.parents[].html_url]}]'
# jqfpy.
$ jsonDATA | 'L = get(); [{"message": get("commit/message", d), "name": get("commit/committer/name", d), "parents": [p["html_url"] for p in d["parents"]]} for d in L]'
# or (using h.pick)
$ jsonDATA | 'L = get(); [h.pick("commit/message@message", "commit/committer/name@name", "parents[]/html_url@parents", d=d) for d in L]'

additionals

other formats support

jqfpy is supporting other formats(but this is experimental feature)

  • yaml
  • ltsv

if you want to use yaml supported version. install via below command.

$ pip install jqfpy[yaml]

and calling jqfpy with --input-format,-i option and --output-format,-o option.

02data.yaml

person:
  name: foo
  age: 20
  nickname: fool
$ cat 02data.yaml | jqfpy -i yaml 'get("person")'
{
  "name": "foo",
  "age": 20,
  "nickname": "fool"
}

$ cat 02data.yaml | jqfpy -i yaml -o ltsv 'get("person")'
name:foo	age:20	nickname:fool

helper functions

helper functions are included.

  • pick()
  • omit()
  • flatten()
  • chunk()
  • loadfile()
  • dumpfile()

pick()

$ cat 02data.yaml | jqfpy -i yaml 'h.pick("person/name", "person/age")'
{
  "person": {
    "name": "foo",
    "age": 20
  }
}

$ cat 02data.yaml | jqfpy -i yaml 'h.pick("person/name@name", "person/age@age")'
{
  "name": "foo",
  "age": 20
}

omit()

$ cat 02data.yaml | jqfpy -i yaml 'h.omit("person/nickname")'
{
  "person": {
    "name": "foo",
    "age": 20
  }
}

flatten()

$ seq 1 5 | jqfpy --slurp -c 'L = get(); [L, L]'
[[1, 2, 3, 4, 5], [1, 2, 3, 4, 5]]
$ seq 1 5 | jqfpy --slurp -c 'L = get(); h.flatten([L, L], n=1)'
[1, 2, 3, 4, 5, 1, 2, 3, 4, 5]

chunk()

$ seq 1 10 | jqfpy --slurp -c 'h.chunk(get(), n=3)'
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10]]

loadfile()

$ ls *.json
a.json b.json
$ echo '["a", "b"]' | jqfpy '{name: h.loadfile(f"{name}.json") for name in get()}'

--here and --relative-path options.

# see ./x.json
$ jqfpy 'h.loadfile("x.json")' a/b/main.json

# see ./a/b/x.json
$ jqfpy --here a/b/ 'h.loadfile("x.json")' a/b/main.json

# see ./a/b/x.json
$ jqfpy --relative-path 'h.loadfile("x.json")' a/b/main.json

dumpfile()

$ echo {"person0.json": {"name": "foo", "age": 20}, "person1.json": {"name": "bar}} | jqfpy '[h.dumpfile(v, fname) for fname, v in get().item()]' > /dev/null

individual helper module with --additionals

match.py

import re


def match(rx, text):
    if text is None:
        return False
    return re.search(rx, text)
$ cat examples/additionals/00data.json | jqfpy --additionals=./match.py '[d for d in get("constraint") if h.match("^1\..+", d.get("version"))]'
[
  {
    "name": "github.com/Masterminds/vcs",
    "version": "1.11.0"
  },
  {
    "name": "github.com/boltdb/bolt",
    "version": "1.0.0"
  }
]

0.6.0

  • add show-none (default, if return value is None, doesn't write to stdout)
  • add raw format
  • add short option -a and -u (drop --ascii-output asa -a)

0.5.4

  • change arguments order, h.dumpfile (#41)
  • fix bug for get() (#37)

0.5.3

  • fix bug for h.dumpfile()

0.5.2

  • add h.loadfile() function (#35)

0.5.1

  • h.dumpfile() with raw option

0.5.0

  • drop python 3.5
  • yaml output, oneline string including hash, always quoted (#28)
  • treat '/foo' as 'foo' (lstripped)
  • add h.dumpfile() function (#31)

0.4.2

  • support python 3.4
  • custom additionals (--additionals option)
  • new helpers -- h.flatten, h.flatten1 and h.chunk

0.4.1

  • ordered is default, when json loading

0.4.0

  • experimental ltsv support
  • adding helper functions (h.omit(), h.pick())
  • extend get() function's function
  • rename option(--show-code-only to --show-code)

0.3.2

  • the future is dropped that showing help when calling with no arguments

0.3.0

  • experimental yaml format support

0.2.0

  • correct behaviours of --slurp and --unbuffered
  • support accessing data by json pointer like format
  • compact output support
  • multiple files support
  • exec code only once

0.1.0

  • adding some options
  • nocode is same as jq . (js == jq .)
  • showing pycode when error is raised

0.0.1

  • first release

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

jqfpy-0.6.0.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

jqfpy-0.6.0-py2.py3-none-any.whl (12.9 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file jqfpy-0.6.0.tar.gz.

File metadata

  • Download URL: jqfpy-0.6.0.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.19.1 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for jqfpy-0.6.0.tar.gz
Algorithm Hash digest
SHA256 b42c34f0cf5e0931c603f29627712420bca0aa063a974e6ff749441bb3b88ea3
MD5 6bc786fb0b91c43e2e626fa98dfea1ff
BLAKE2b-256 be0f098dae22a38e063a1caaea608f432357d8e5adfdfae20177a59e8fd6ef6b

See more details on using hashes here.

File details

Details for the file jqfpy-0.6.0-py2.py3-none-any.whl.

File metadata

  • Download URL: jqfpy-0.6.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.19.1 setuptools/41.0.1 requests-toolbelt/0.8.0 tqdm/4.31.1 CPython/3.7.3

File hashes

Hashes for jqfpy-0.6.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 d742065b191f217504b05dcd23564f1edabd76bc3a1a869b170cfc769166f144
MD5 b637b82d2cf8bb8b929f9f0cba7968f9
BLAKE2b-256 34e6c3e520b4ec2fec845a846ce0464691296cc8f61bafefaa9e479746091bc7

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