Skip to main content

pq is a Python command-line JSON processor

Project description

pq-json

pq is a Python command-line JSON processor - almost like (atleast inspired by 🐶️) jq, but using Python focusing on simplicity and convenience with familiar syntax.

Install

pip install pq-json

Here is a simple example for parsing JSON. Output from pq is pretty printed using Rich.

$ echo '{"text": "Text here", "header": "Header", "list": [1,2,3]}' | pq
{
  "text": "Text here",
  "header": "Header",
  "list": [
    1,
    2,
    3
  ]
}

Pretty Parse

Filters

The processing is handled with filters, like in jq. j represents the current input object in a filter.

$ echo '{"example": "data", "nothing": "interesting"}' | pq "j['example']"
"data"
$ echo '{"example": "data", "nothing": "interesting"}' | pq "j['example']"
"data"

As default, None will not be passed.

$ echo '{"example": "data", "nothing": "interesting"}' | pq "j.get('nada')"

List slicing

JSON arrays are converted to Python list.

$ echo '[{"name": "eric", "age": 22}, {"name": "daniel", "age": 44}]' | pq "j[0]"
{
  "name": "eric",
  "age": 22
}
$ echo '[{"name": "eric", "age": 22}, {"name": "daniel", "age": 44}]' | pq "j[-1]"
{
  "name": "daniel",
  "age": 44
}

An array always iterates to the next filter. Here we are using the slice symbol [:] to highlight that we are working with an array.

$ echo '[{"name": "eric", "age": 22}, {"name": "daniel", "age": 44}]' | pq "j[:]"
{
  "name": "eric",
  "age": 22
}
{
  "name": "daniel",
  "age": 44
}

Want to send the full array to the next filter? Wrap array around [] brackets like:

$ echo '[1,2,3,4,5]' | pq "[j[:]]"
[
  1,
  2,
  3,
  4,
  5
]

Pipes

Pipes let you chain multiple filters by produce output to the filter to the right of the pipe. Under the hood a pipeline is a chain of generators. An array will for example yield multiple elements to the right.

input:

["a", "b", "c", "d"]
pq "j[0:2] | j.upper()"
      |         |
   filter1   filter2

produces the following result:
"A"
"B"

In this case, the expression j.upper() will be run each time.

j[0] -> "a".upper() -> "A"
j[1] -> "b".upper() -> "B"

Another example:

$ echo '[1,2,3,4,5,6,7,8,9]' | pq 'j[:] | j**2+50'
51
54
59
66
75
86
99
114
131

Array constructs

Above example outputs a list of integers. It's possible to wrap it all into a single array by using [] around the full expression.

$ echo '[1,2,3,4,5,6,7,8,9]' | pq '[j[:] | j**2+50]'
[51, 54, 59, 66, 75, 86, 99, 114, 131]

Object constructs

$ echo '{"name":"jan", "age":4, "parents": ["lisa", "dan"]}' | pq '{"name": j["name"], "parents": j["parents"]}'
{
  "name": "jan",
  "parents": [
    "lisa",
    "dan"
  ]
}

Custom modules

It's possible to add additional modules to global scope from file or input string. We can declare a variable for example.

echo '[1,2,3]' | pq --module 'two=2' 'j[1]*two'
4

Other examples

We can easily use built-in functions in Python

$ echo "[1,2,3,4,5,6]" | pq 'sum(j) | {"total": j}'
{
  "total": 21
}

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

pq-json-0.0.3.1.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pq_json-0.0.3.1-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file pq-json-0.0.3.1.tar.gz.

File metadata

  • Download URL: pq-json-0.0.3.1.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for pq-json-0.0.3.1.tar.gz
Algorithm Hash digest
SHA256 bc1a3cf748f4e3a74f770b9a34373d6933ab9be3c9508adab6ca8a7b33c07d3c
MD5 b5dbb7c1c694a565c6b833f0b96c9d00
BLAKE2b-256 ee7528f1e724825a5431ad646b8272f60fbebfc1912ee7027fe96cef1ec124a3

See more details on using hashes here.

File details

Details for the file pq_json-0.0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pq_json-0.0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.0 CPython/3.8.10

File hashes

Hashes for pq_json-0.0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9c2f5400340df8b7b530f98082ed437dccd1bf814c319a3e6bba457df8e2fff0
MD5 b1fb18a8d360ed7d38d227141bb98d07
BLAKE2b-256 c7b8e02eea448fce4e5816937564317b968f30b932dcb40d9da4bdb42c3b0105

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page