{"id":15867262,"url":"https://github.com/antonbabenko/pyjq","last_synced_at":"2025-04-01T21:27:48.210Z","repository":{"id":102581831,"uuid":"260651446","full_name":"antonbabenko/pyjq","owner":"antonbabenko","description":"Previously private fork of https://github.com/doloopwhile/pyjq with jq 1.6","archived":false,"fork":false,"pushed_at":"2020-05-04T02:24:21.000Z","size":7624,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-02-14T14:56:46.796Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/antonbabenko.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2020-05-02T09:12:23.000Z","updated_at":"2022-07-19T16:04:39.000Z","dependencies_parsed_at":null,"dependency_job_id":"265ef8a3-a3f9-4588-8cae-5e5dfe392b4a","html_url":"https://github.com/antonbabenko/pyjq","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonbabenko%2Fpyjq","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonbabenko%2Fpyjq/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonbabenko%2Fpyjq/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/antonbabenko%2Fpyjq/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/antonbabenko","download_url":"https://codeload.github.com/antonbabenko/pyjq/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246712351,"owners_count":20821715,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-05T23:41:59.288Z","updated_at":"2025-04-01T21:27:48.193Z","avatar_url":"https://github.com/antonbabenko.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"pyjq: Binding for jq JSON Processor\n===================================\n\n[![CircleCI](https://circleci.com/gh/doloopwhile/pyjq.svg?style=svg)](https://circleci.com/gh/doloopwhile/pyjq)\n\npyjq is a Python bindings for jq (\u003chttp://stedolan.github.io/jq/\u003e).\n\n\u003e jq is like sed for JSON data - you can use it to slice and filter and\n\u003e map and transform structured data with the same ease that sed, awk,\n\u003e grep and friends let you play with text.\n\u003e\n\u003e \u003chttp://stedolan.github.io/jq/\u003e\n\nYou can seamlessly call jq script (like regular expression) and process\na plain python data structure.\n\nFor your information, \u003chttps://pypi.python.org/pypi/jq\u003e is a also jq\nbindings but different and incompatible with pyjq.\n\nExample\n-------\n\n```python\n\u003e\u003e\u003e data = dict(\n...     parameters= [\n...         dict(name=\"PKG_TAG_NAME\", value=\"trunk\"),\n...         dict(name=\"GIT_COMMIT\", value=\"master\"),\n...         dict(name=\"TRIGGERED_JOB\", value=\"trunk-buildall\")\n...     ],\n...     id=\"2013-12-27_00-09-37\",\n...     changeSet=dict(items=[], kind=\"git\"),\n... )\n\u003e\u003e\u003e import pyjq\n\u003e\u003e\u003e pyjq.first('.parameters[] | {\"param_name\": .name, \"param_type\":.type}', data)\n{'param_name': 'PKG_TAG_NAME', 'param_type': None}\n\n```\n\nInstall\n-------\n\nIt requires build tools such as make, automake, libtool, etc...\n\nYou can install from PyPI by usual way.\n\n```shell\npip install pyjq\n```\n\nAPI\n---\n\nFor jq script, [see its manual](http://stedolan.github.io/jq/manual/).\n\nOnly four APIs are provided:\n\n- `all`\n- `first`\n- `one`\n- `compile`\n\n`all` transforms a value by JSON script and returns all results as a list.\n\n```python\n\u003e\u003e\u003e value = {\"user\":\"stedolan\",\"titles\":[\"JQ Primer\", \"More JQ\"]}\n\u003e\u003e\u003e pyjq.all('{user, title: .titles[]}', value)\n[{'user': 'stedolan', 'title': 'JQ Primer'}, {'user': 'stedolan', 'title': 'More JQ'}]\n```\n\n`all` takes an optional argument `vars`.\n`vars` is a dictonary of predefined variables for `script`.\nThe values in `vars` are available in the `script` as a `$key`.\nThat is, `vars` works like `--arg` option and `--argjson` option of jq command.\n\n```python\n\u003e\u003e\u003e pyjq.all('{user, title: .titles[]} | select(.title == $title)', value, vars={\"title\": \"More JQ\"})\n[{'user': 'stedolan', 'title': 'More JQ'}]\n```\n\n`all` takes an optional argument `url`.\nIf `url` is given, the subject of transformation is retrieved from the `url`.\n\n```python\n\u003e\u003e pyjq.all(\".[] | .login\", url=\"https://api.github.com/repos/stedolan/jq/contributors\") # get all contributors of jq\n['nicowilliams', 'stedolan', 'dtolnay', ... ]\n```\n\nAdditionally, `all` takes an optional argument `opener`.\nThe default `opener` will download contents using `urllib.request.urlopen` and decode using `json.decode`.\nHowever, you can customize this behavior using a custom `opener`.\n\n`first` and `one` are similar to to `all`.\n\n`first` returns the first result of transformation.\nWhen there are no results, `first` returns `None` or the given `default`.\n\n```python\n\u003e\u003e\u003e data = {\"user\":\"stedolan\",\"titles\":[\"JQ Primer\", \"More JQ\"]}\n\u003e\u003e\u003e pyjq.first('{user, title: .titles[]}', data)\n{'user': 'stedolan', 'title': 'JQ Primer'}\n\u003e\u003e\u003e pyjq.first('.titles[] | select(test(\"T\"))', data) # returns None\n\u003e\u003e\u003e pyjq.first('.titles[] | select(test(\"T\"))', data, default=\"Third JS\")\n'Third JS'\n```\n\n`one` returns the only result of a transformation.\nIt raises an exception when there are no results or when there are two or more results.\n\n```python\n\u003e\u003e\u003e data = {\"user\":\"stedolan\",\"titles\": [\"JQ Primer\", \"More JQ\"]}\n\u003e\u003e\u003e pyjq.one('.titles[] | select(test(\"P\"))', data)\n'JQ Primer'\n\u003e\u003e\u003e pyjq.one('.titles[] | select(test(\"T\"))', data)\nTraceback (most recent call last):\nIndexError: Result of jq is empty\n\u003e\u003e\u003e pyjq.one('.titles[] | select(test(\"J\"))', data)\nTraceback (most recent call last):\nIndexError: Result of jq have multiple elements\n```\n\n`compile` is similar to `re.compile`. It accepts jq script and returns an object with methods.\n\n```python\n\u003e\u003e\u003e data = {\"user\":\"stedolan\",\"titles\":[\"JQ Primer\", \"More JQ\"]}\n\u003e\u003e\u003e import pyjq\n\u003e\u003e\u003e pat = pyjq.compile('{user, title: .titles[]}')\n\u003e\u003e\u003e pat.all(data)\n[{'user': 'stedolan', 'title': 'JQ Primer'}, {'user': 'stedolan', 'title': 'More JQ'}]\n```\n\nLimitations\n-----------\n\njq is a JSON Processor. Therefore pyjq is able to process only\n\"JSON compatible\" data (object made only from str, int, float, list, dict).\n\nQ\u0026A\n---\n\n### How can I process a json string (f.e. gotten from an API) with pyjq?\n\nYou should call `json.loads` from the standard library on the string, before you pass it to pyjq.\n\nAuthor\n------\n[OMOTO Kenji](https://github.com/doloopwhile)\n\nLicense\n-------\n\nReleased under the MIT license. See LICENSE for details.\n\nDevelopment\n-----------\n\n## Pipenv\n\nThis project uses [Pipenv](https://docs.pipenv.org/en/latest/) to manage dependencies.\n\nPlease install development tools with the following command:\n\n```shell\npipenv install --dev -e\n```\n\n## Test\n\nWe can run the tests with `tox`.\n\n```shell\npipenv run pytest --doctest-modules --ignore-glob='dependencies/**/*.py'\n```\n\nOn pull request, Tox is executed in Circle CI.\n\n## We DO commit `_pyjq.c`\n\nWhen you edit `_pyjq.pyx`, you need to run `pipenv run cython _pyjq.pyx` before you run `pipenv run python setup.py develop`.\nYou need to do this because `setup.py` in this project does not compile `.pyx` to `.c` .\n\nOf course, we can use `Cython.Build.cythonize` in `setup.py` to automatically compile `.pyx` to `.c` .\nBut, it causes a bootstrap problem in ``pip install``.\n\nSo, we DO commit both of `_pyjq.pyx` and `_pyjq.c`.\n\nLicense\n-------\nMIT License. See [LICENSE](./LICENSE).\n\nThis package includes [jq](https://github.com/stedolan/jq) and [oniguruma](https://github.com/kkos/oniguruma). Their license files are included in their respective archive files.\n\n- jq: `dependencies/jq-1.5.tar.gz`\n- oniguruma: `dependencies/onig-6.9.0.tar.gz`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantonbabenko%2Fpyjq","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fantonbabenko%2Fpyjq","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fantonbabenko%2Fpyjq/lists"}