{"id":22542490,"url":"https://github.com/jsonquerylang/jsonquery-python","last_synced_at":"2025-04-14T19:22:30.300Z","repository":{"id":261772577,"uuid":"885014300","full_name":"jsonquerylang/jsonquery-python","owner":"jsonquerylang","description":"A lightweight, flexible, and expandable JSON query language","archived":false,"fork":false,"pushed_at":"2025-04-14T11:04:26.000Z","size":103,"stargazers_count":8,"open_issues_count":1,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-04-14T12:22:51.549Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jsonquerylang.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","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,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2024-11-07T19:51:26.000Z","updated_at":"2025-04-10T04:41:07.000Z","dependencies_parsed_at":"2025-02-02T08:27:45.904Z","dependency_job_id":"f586045f-d700-4f76-9539-92ce3aa6df8e","html_url":"https://github.com/jsonquerylang/jsonquery-python","commit_stats":null,"previous_names":["jsonquerylang/jsonquery-python"],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonquerylang%2Fjsonquery-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonquerylang%2Fjsonquery-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonquerylang%2Fjsonquery-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsonquerylang%2Fjsonquery-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsonquerylang","download_url":"https://codeload.github.com/jsonquerylang/jsonquery-python/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248943415,"owners_count":21186958,"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-12-07T13:10:35.834Z","updated_at":"2025-04-14T19:22:30.277Z","avatar_url":"https://github.com/jsonquerylang.png","language":"Python","readme":"# jsonquery-python\n\n![JSON Query Logo](https://jsonquerylang.org/frog-756900-100.png)\n\nThis is a Python implementation of **JSON Query**, a small, flexible, and expandable JSON query language.\n\nTry it out on the online playground: \u003chttps://jsonquerylang.org\u003e\n\n![JSON Query Overview](https://jsonquerylang.org/jsonquery-overview.svg)\n\n## Install\n\nInstall via PyPi: https://pypi.org/project/jsonquerylang/\n\n```\npip install jsonquerylang\n```\n\n## Use\n\n```python\nfrom jsonquerylang import jsonquery\nfrom pprint import pprint\n\ndata = {\n    \"friends\": [\n        {\"name\": \"Chris\", \"age\": 23, \"city\": \"New York\"},\n        {\"name\": \"Emily\", \"age\": 19, \"city\": \"Atlanta\"},\n        {\"name\": \"Joe\", \"age\": 32, \"city\": \"New York\"},\n        {\"name\": \"Kevin\", \"age\": 19, \"city\": \"Atlanta\"},\n        {\"name\": \"Michelle\", \"age\": 27, \"city\": \"Los Angeles\"},\n        {\"name\": \"Robert\", \"age\": 45, \"city\": \"Manhattan\"},\n        {\"name\": \"Sarah\", \"age\": 31, \"city\": \"New York\"}\n    ]\n}\n\n# Get the array containing the friends from the object, filter the friends that live in New York,\n# sort them by age, and pick just the name and age out of the objects.\noutput = jsonquery(data, \"\"\"\n    .friends \n        | filter(.city == \"New York\") \n        | sort(.age) \n        | pick(.name, .age)\n\"\"\")\npprint(output)\n# [{'age': 23, 'name': 'Chris'},\n#  {'age': 31, 'name': 'Sarah'},\n#  {'age': 32, 'name': 'Joe'}]\n\n# The same query can be written using the JSON format instead of the text format.\n# Note that the functions `parse` and `stringify` can be used\n# to convert from text format to JSON format and vice versa.\npprint(jsonquery(data, [\n    \"pipe\",\n    [\"get\", \"friends\"],\n    [\"filter\", [\"eq\", [\"get\", \"city\"], \"New York\"]],\n    [\"sort\", [\"get\", \"age\"]],\n    [\"pick\", [\"get\", \"name\"], [\"get\", \"age\"]]\n]))\n# [{'age': 23, 'name': 'Chris'},\n#  {'age': 31, 'name': 'Sarah'},\n#  {'age': 32, 'name': 'Joe'}]\n```\n\n### Syntax\n\nThe JSON Query syntax is described on the following page: https://github.com/jsonquerylang/jsonquery?tab=readme-ov-file#syntax.\n\n## API\n\n### jsonquery\n\nCompile and evaluate a JSON query.\n\nSyntax:\n\n```\njsonquery(data, query [, options])\n```\n\nWhere:\n\n- `data` is a JSON object or array\n- `query` is a JSON query or string containing a text query\n- `options` is an optional object which can have the following options:\n  - `functions` an object with custom functions\n\nExample:\n\n```python\nfrom pprint import pprint\nfrom jsonquerylang import jsonquery\n\ninput = [\n    {\"name\": \"Chris\", \"age\": 23, \"scores\": [7.2, 5, 8.0]},\n    {\"name\": \"Joe\", \"age\": 32, \"scores\": [6.1, 8.1]},\n    {\"name\": \"Emily\", \"age\": 19},\n]\nquery = [\"sort\", [\"get\", \"age\"], \"desc\"]\noutput = jsonquery(input, query)\npprint(output)\n# [{'age': 32, 'name': 'Joe', 'scores': [6.1, 8.1]},\n#  {'age': 23, 'name': 'Chris', 'scores': [7.2, 5, 8.0]},\n#  {'age': 19, 'name': 'Emily'}]\n```\n\n### compile\n\nCompile a JSON Query. Returns a function which can execute the query repeatedly for different inputs.\n\nSyntax:\n\n```\ncompile(query [, options])\n```\n\nWhere:\n\n- `query` is a JSON query or string containing a text query\n- `options` is an optional object which can have the following options:\n  - `functions` an object with custom functions\n\nThe function returns a lambda function which can be executed by passing JSON data as first argument.\n\nExample:\n\n```python\nfrom pprint import pprint\nfrom jsonquerylang import compile\n\ninput = [\n    {\"name\": \"Chris\", \"age\": 23, \"scores\": [7.2, 5, 8.0]},\n    {\"name\": \"Joe\", \"age\": 32, \"scores\": [6.1, 8.1]},\n    {\"name\": \"Emily\", \"age\": 19},\n]\nquery = [\"sort\", [\"get\", \"age\"], \"desc\"]\nqueryMe = compile(query)\noutput = queryMe(input)\npprint(output)\n# [{'age': 32, 'name': 'Joe', 'scores': [6.1, 8.1]},\n#  {'age': 23, 'name': 'Chris', 'scores': [7.2, 5, 8.0]},\n#  {'age': 19, 'name': 'Emily'}]\n```\n\n### parse\n\nParse a string containing a JSON Query into JSON.\n\nSyntax:\n\n```\nparse(textQuery, [, options]) \n```\n\nWhere: \n\n- `textQuery`: A query in text format\n- `options`: An optional object which can have the following properties:\n  - `functions` an object with custom functions\n  - `operators` an object with the names of custom operators both as key and value\n\nExample:\n\n```python\nfrom pprint import pprint\nfrom jsonquerylang import parse\n\ntext_query = '.friends | filter(.city == \"new York\") | sort(.age) | pick(.name, .age)'\njson_query = parse(text_query)\npprint(json_query)\n# ['pipe',\n#  ['get', 'friends'],\n#  ['filter', ['eq', ['get', 'city'], 'New York']],\n#  ['sort', ['get', 'age']],\n#  ['pick', ['get', 'name'], ['get', 'age']]]\n```\n\n### stringify\n\nStringify a JSON Query into a readable, human friendly text format.\n\nSyntax:\n\n```\nstringify(query [, options])\n```\n\nWhere:\n\n- `query` is a JSON Query\n- `options` is an optional object that can have the following properties:\n  - `operators` an object with the names of custom operators both as key and value\n  - `indentation` a string containing the desired indentation, defaults to two spaces: `\"  \"`\n  - `max_line_length` a number with the maximum line length, used for wrapping contents. Default value: `40`.\n\nExample:\n\n```python\nfrom jsonquerylang import stringify\n\njsonQuery = [\n    \"pipe\",\n    [\"get\", \"friends\"],\n    [\"filter\", [\"eq\", [\"get\", \"city\"], \"New York\"]],\n    [\"sort\", [\"get\", \"age\"]],\n    [\"pick\", [\"get\", \"name\"], [\"get\", \"age\"]],\n]\ntextQuery = stringify(jsonQuery)\nprint(textQuery)\n# '.friends | filter(.city == \"new York\") | sort(.age) | pick(.name, .age)'\n```\n\n## License\n\nReleased under the [ISC license](LICENSE.md).\n","funding_links":[],"categories":["Python"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonquerylang%2Fjsonquery-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsonquerylang%2Fjsonquery-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsonquerylang%2Fjsonquery-python/lists"}