jq

Python bindings for jq

https://github.com/mwilliamson/jq.py

Science Score: 26.0%

This score indicates how likely this project is to be science-related based on various indicators:

  • CITATION.cff file
  • codemeta.json file
    Found codemeta.json file
  • .zenodo.json file
    Found .zenodo.json file
  • DOI references
  • Academic publication links
  • Committers with academic emails
  • Institutional organization owner
  • JOSS paper metadata
  • Scientific vocabulary similarity
    Low similarity (11.2%) to scientific vocabulary

Keywords from Contributors

mqtt-publisher notification
Last synced: 10 months ago · JSON representation

Repository

Python bindings for jq

Basic Info
  • Host: GitHub
  • Owner: mwilliamson
  • License: bsd-2-clause
  • Language: Python
  • Default Branch: master
  • Size: 10.7 MB
Statistics
  • Stars: 404
  • Watchers: 7
  • Forks: 58
  • Open Issues: 22
  • Releases: 0
Created about 13 years ago · Last pushed 12 months ago
Metadata Files
Readme Changelog License

README.rst

jq.py: a lightweight and flexible JSON processor
================================================

This project contains Python bindings for
`jq `_ 1.8.1.

Installation
------------

Wheels are built for various Python versions and architectures on Linux and Mac OS X.
On these platforms, you should be able to install jq with a normal pip install:

.. code-block:: sh

    pip install jq

If a wheel is not available,
the source for jq 1.8.1 is built.
This requires:

* Autoreconf

* The normal C compiler toolchain, such as gcc and make.

* libtool

* Python headers.

Alternatively, set the environment variable ``JQPY_USE_SYSTEM_LIBS`` to ``1`` when installing the package
to use the libjq and libonig versions available on the system rather than building them.

Debian, Ubuntu or relatives
~~~~~~~~~~~~~~~~~~~~~~~~~~~

If on Debian, Ubuntu or relatives, running the following command should be sufficient:

.. code-block:: sh

    apt-get install autoconf automake build-essential libtool python-dev

Red Hat, Fedora, CentOS or relatives
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

If on Red Hat, Fedora, CentOS, or relatives, running the following command should be sufficient:

.. code-block:: sh

    yum groupinstall "Development Tools"
    yum install autoconf automake libtool python python-devel

Mac OS X
~~~~~~~~

If on Mac OS X, you probably want to install
`Xcode `_ and `Homebrew `_.
Once Homebrew is installed, you can install the remaining dependencies with:

.. code-block:: sh

    brew install autoconf automake libtool

Usage
-----

Using jq requires three steps:

#. Call ``jq.compile()`` to compile a jq program.
#. Call an input method on the compiled program to supply the input.
#. Call an output method on the result to retrieve the output.

For instance:

.. code-block:: python

    import jq

    assert jq.compile(".+5").input_value(42).first() == 47

Input methods
~~~~~~~~~~~~~

Call ``.input_value()`` to supply a valid JSON value, such as the values returned from ``json.load``:

.. code-block:: python

    import jq

    assert jq.compile(".").input_value(None).first() == None
    assert jq.compile(".").input_value(42).first() == 42
    assert jq.compile(".").input_value(0.42).first() == 0.42
    assert jq.compile(".").input_value(True).first() == True
    assert jq.compile(".").input_value("hello").first() == "hello"

Call ``.input_values()`` to supply multiple valid JSON values, such as the values returned from ``json.load``:

.. code-block:: python

    import jq

    assert jq.compile(".+5").input_values([1, 2, 3]).all() == [6, 7, 8]

Call ``.input_text()`` to supply unparsed JSON text:

.. code-block:: python

    import jq

    assert jq.compile(".").input_text("null").first() == None
    assert jq.compile(".").input_text("42").first() == 42
    assert jq.compile(".").input_text("0.42").first() == 0.42
    assert jq.compile(".").input_text("true").first() == True
    assert jq.compile(".").input_text('"hello"').first() == "hello"
    assert jq.compile(".").input_text("1\n2\n3").all() == [1, 2, 3]

Pass ``slurp=True`` to ``.input_text()`` to read the entire input into an array:

.. code-block:: python

    import jq

    assert jq.compile(".").input_text("1\n2\n3", slurp=True).first() == [1, 2, 3]

You can also call the older ``input()`` method by passing:

* a valid JSON value, such as the values returned from ``json.load``, as a positional argument
* unparsed JSON text as the keyword argument ``text``

For instance:

.. code-block:: python

    import jq

    assert jq.compile(".").input("hello").first() == "hello"
    assert jq.compile(".").input(text='"hello"').first() == "hello"

Output methods
~~~~~~~~~~~~~~

Calling ``first()`` on the result will run the program with the given input,
and return the first output element.

.. code-block:: python

    import jq

    assert jq.compile(".").input_value("hello").first() == "hello"
    assert jq.compile("[.[]+1]").input_value([1, 2, 3]).first() == [2, 3, 4]
    assert jq.compile(".[]+1").input_value([1, 2, 3]).first() == 2

Call ``text()`` instead of ``first()`` to serialise the output into JSON text:

.. code-block:: python

    assert jq.compile(".").input_value("42").text() == '"42"'

When calling ``text()``, if there are multiple output elements, each element is represented by a separate line:

.. code-block:: python

    assert jq.compile(".[]").input_value([1, 2, 3]).text() == "1\n2\n3"

Call ``all()`` to get all of the output elements in a list:

.. code-block:: python

    assert jq.compile(".[]+1").input_value([1, 2, 3]).all() == [2, 3, 4]

Call ``iter()`` to get all of the output elements as an iterator:

.. code-block:: python

    iterator = iter(jq.compile(".[]+1").input_value([1, 2, 3]))
    assert next(iterator, None) == 2
    assert next(iterator, None) == 3
    assert next(iterator, None) == 4
    assert next(iterator, None) == None

Arguments
~~~~~~~~~

Calling ``compile()`` with the ``args`` argument allows predefined variables to be used within the program:

.. code-block:: python

    program = jq.compile("$a + $b + .", args={"a": 100, "b": 20})
    assert program.input_value(3).first() == 123

Convenience functions
~~~~~~~~~~~~~~~~~~~~~

Convenience functions are available to get the output for a program and input in one call:

.. code-block:: python

    assert jq.first(".[] + 1", [1, 2, 3]) == 2
    assert jq.first(".[] + 1", text="[1, 2, 3]") == 2
    assert jq.text(".[] + 1", [1, 2, 3]) == "2\n3\n4"
    assert jq.all(".[] + 1", [1, 2, 3]) == [2, 3, 4]
    assert list(jq.iter(".[] + 1", [1, 2, 3])) == [2, 3, 4]

Original program string
~~~~~~~~~~~~~~~~~~~~~~~

The original program string is available on a compiled program as the ``program_string`` attribute:

.. code-block:: python

    program = jq.compile(".")
    assert program.program_string == "."

Owner

  • Name: Michael Williamson
  • Login: mwilliamson
  • Kind: user
  • Location: Cambridge, UK

GitHub Events

Total
  • Create event: 6
  • Commit comment event: 1
  • Issues event: 7
  • Watch event: 42
  • Delete event: 4
  • Issue comment event: 11
  • Push event: 25
  • Pull request event: 3
  • Fork event: 5
Last Year
  • Create event: 6
  • Commit comment event: 1
  • Issues event: 7
  • Watch event: 42
  • Delete event: 4
  • Issue comment event: 11
  • Push event: 25
  • Pull request event: 3
  • Fork event: 5

Committers

Last synced: over 1 year ago

All Time
  • Total Commits: 307
  • Total Committers: 11
  • Avg Commits per committer: 27.909
  • Development Distribution Score (DDS): 0.075
Past Year
  • Commits: 33
  • Committers: 4
  • Avg Commits per committer: 8.25
  • Development Distribution Score (DDS): 0.273
Top Committers
Name Email Commits
Michael Williamson m****e@z****g 284
lando d****9@q****m 5
Nikolai Kondrashov N****v@r****m 4
Morten Høybye Frederiksen m****n@m****k 3
Marc Abramowitz m****c@m****m 3
Gabriel Dugny g****t@d****e 3
Zach Probst Z****t@i****m 1
Stefan Seemayer s****n@s****e 1
Shivani Bhardwaj s****4@g****m 1
Fabian Affolter m****l@f****h 1
Marc Abramowitz m****a@s****m 1

Issues and Pull Requests

Last synced: 10 months ago

All Time
  • Total issues: 82
  • Total pull requests: 38
  • Average time to close issues: 8 months
  • Average time to close pull requests: 5 months
  • Total issue authors: 74
  • Total pull request authors: 28
  • Average comments per issue: 4.11
  • Average comments per pull request: 3.24
  • Merged pull requests: 12
  • Bot issues: 0
  • Bot pull requests: 0
Past Year
  • Issues: 7
  • Pull requests: 3
  • Average time to close issues: 2 days
  • Average time to close pull requests: 8 days
  • Issue authors: 7
  • Pull request authors: 2
  • Average comments per issue: 1.14
  • Average comments per pull request: 1.67
  • Merged pull requests: 0
  • Bot issues: 0
  • Bot pull requests: 0
Top Authors
Issue Authors
  • mandree (4)
  • MuffintopBikini (3)
  • mborsetti (2)
  • nicowilliams (2)
  • itcarroll (2)
  • solidsnack (1)
  • rabeeqasem (1)
  • Arithmomaniac (1)
  • zhuangxy (1)
  • colhndchck (1)
  • Lazerbeak12345 (1)
  • DEV-ONI (1)
  • kapilt (1)
  • mslinn (1)
  • witte-de-with (1)
Pull Request Authors
  • spbnick (8)
  • msabramo (4)
  • GabDug (2)
  • henryiii (2)
  • Viicos (2)
  • woky (2)
  • du33169 (2)
  • FantasqueX (2)
  • timfel (2)
  • sseemayer (1)
  • inashivb (1)
  • futoase (1)
  • odidev (1)
  • zprobst (1)
  • mortenf (1)
Top Labels
Issue Labels
enhancement (1)
Pull Request Labels

Packages

  • Total packages: 1
  • Total downloads:
    • pypi 3,030,917 last-month
  • Total docker downloads: 3,865,589
  • Total dependent packages: 118
  • Total dependent repositories: 761
  • Total versions: 41
  • Total maintainers: 1
pypi.org: jq

jq is a lightweight and flexible JSON processor.

  • Versions: 41
  • Dependent Packages: 118
  • Dependent Repositories: 761
  • Downloads: 3,030,917 Last month
  • Docker Downloads: 3,865,589
Rankings
Dependent packages count: 0.2%
Downloads: 0.3%
Dependent repos count: 0.4%
Docker downloads count: 0.7%
Average: 1.9%
Stargazers count: 3.7%
Forks count: 6.0%
Maintainers (1)
Last synced: 10 months ago

Dependencies

test-requirements.txt pypi
  • pytest * test
.github/workflows/build.yml actions
  • actions/checkout v2 composite
  • actions/setup-python v4 composite
  • actions/upload-artifact v2 composite
  • docker/setup-qemu-action v1 composite
  • pypa/cibuildwheel v2.11.4 composite
pyproject.toml pypi
setup.py pypi