7

In Ubuntu you can simply install jq:

apt-get install jq 

But, on Debian it is not in the repository.

Is there an alternative command-line to parse a JSON string on the console?

maybe you can achieve this with python like (fantasycode):

cat test.json | python 'parse_json(stdin)'
1

8 Answers 8

6

jq is in Wheezy backports, and slated for Jessie

For backports on Wheezy, add the following to /etc/apt/sources.list (or a file to sources.d/???.list)

deb http://http.debian.net/debian wheezy-backports main

Then:

apt-get -t wheezy-backports install jq

Another alternative:

python -m json.tool file.json
3

One CLI json parser is jshon. Here is a simple example:

$ jshon -e foo -u <<< '{ "foo":"bar" }' 
bar

The -e options extract values from the json and the -u decodes the final string.

2

also take a look at jtc unix utility - I developed jtc aiming to simply achieving JSON queries drastically:

  • it's objectively faster
  • easier to attain a solution (in most of the cases) - though it's subjective, until you try
  • the base concept is easy to understand
  • typically, jtc idiomatic solutions would be input data omissions resistant and invariant to input data outer structure (as long walking JSON elements retain the same relationship)
1

jp is one JSON parsing CLI alternative to jq based on the Python library jmespath (a pretty solid tool to depend on, it's notably the default JSON parsing library for the AWS cli).

While the library in question seems to be available on Debian, the jp CLI interface however doesn't seem to be.

0
0

As my search for something like this ended me up here twice now.

A working python solution that also supports filtering like:

cat test.json | jq -r .myfield

would be:

cat test.json | python -c 'import json, sys; print(json.dumps(json.loads("".join([l for l in sys.stdin]))["myfield"]))'
0

I created a cli tool called jello that works similarly to jq but allows you to use pure python code in your filters without all of the JSON loading boilerplate. Also allows you to use dot notation instead of dict bracket notation if you choose.

$ ip -j addr show | jello -lr '\
ips = []
for interface in _:
  for addr in interface.addr_info:
    ips.append(addr.get("local"))
ips'

127.0.0.1
::1
192.168.1.228
2600:1700:bab0:d40::49
2600:1700:bab0:d40:368d:e89e:d304:e083
fe80::9273:6fce:6df6:8f56

https://github.com/kellyjonbrazil/jello

-1

Take a look at fx 🔥

It's plain JavaScript cli JSON manipulating tool (alternative to jq) with standalone binary.

program | fx 'input => input.filter(x => ...).map(x => {a: a.x})'
1
  • Also not available for Debian from ordinary apt repos.
    – Kusalananda
    Commented May 14 at 8:23
-1

I also ended up here looking for a json command line parser which name I didn't remember :)

Turns out noone mentioned what I was looking for, so let me tell you about jid - the

json incremental digger

1
  • Please add an example how to install and how to use it. And no need for the first sentence, delete it
    – rubo77
    Commented Sep 3, 2021 at 9:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .