{"id":13625778,"url":"https://github.com/amirziai/flatten","last_synced_at":"2025-10-15T07:41:02.707Z","repository":{"id":45240703,"uuid":"55542695","full_name":"amirziai/flatten","owner":"amirziai","description":"Flatten JSON in Python","archived":false,"fork":false,"pushed_at":"2023-11-01T03:10:14.000Z","size":136,"stargazers_count":546,"open_issues_count":40,"forks_count":99,"subscribers_count":9,"default_branch":"master","last_synced_at":"2025-04-16T12:19:38.582Z","etag":null,"topics":["flattened-objects","flattens","json","pandas","python","unflatten"],"latest_commit_sha":null,"homepage":"https://pypi.python.org/pypi/flatten_json","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/amirziai.png","metadata":{"files":{"readme":"README.md","changelog":null,"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}},"created_at":"2016-04-05T20:16:25.000Z","updated_at":"2025-04-15T18:12:46.000Z","dependencies_parsed_at":"2022-09-07T04:10:09.571Z","dependency_job_id":"b8efc254-1fbc-4d02-83a9-1f4f159e1d2e","html_url":"https://github.com/amirziai/flatten","commit_stats":{"total_commits":119,"total_committers":19,"mean_commits":"6.2631578947368425","dds":0.546218487394958,"last_synced_commit":"467934f7a8ecfdbc5a3100cfd042bea54140075a"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/amirziai/flatten","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amirziai%2Fflatten","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amirziai%2Fflatten/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amirziai%2Fflatten/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amirziai%2Fflatten/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/amirziai","download_url":"https://codeload.github.com/amirziai/flatten/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/amirziai%2Fflatten/sbom","scorecard":{"id":189806,"data":{"date":"2025-08-11","repo":{"name":"github.com/amirziai/flatten","commit":"900c4feebcfcd831ddb6143c4b246024ecf911ec"},"scorecard":{"version":"v5.2.1-40-gf6ed084d","commit":"f6ed084d17c9236477efd66e5b258b9d4cc7b389"},"score":4,"checks":[{"name":"Dangerous-Workflow","score":10,"reason":"no dangerous workflow patterns detected","details":null,"documentation":{"short":"Determines if the project's GitHub Action workflows avoid dangerous patterns.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#dangerous-workflow"}},{"name":"Maintained","score":0,"reason":"0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0","details":null,"documentation":{"short":"Determines if the project is \"actively maintained\".","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#maintained"}},{"name":"Token-Permissions","score":0,"reason":"detected GitHub workflow tokens with excessive permissions","details":["Warn: no topLevel permission defined: .github/workflows/python-package.yml:1","Info: no jobLevel write permissions found"],"documentation":{"short":"Determines if the project's workflows follow the principle of least privilege.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#token-permissions"}},{"name":"Packaging","score":-1,"reason":"packaging workflow not detected","details":["Warn: no GitHub/GitLab publishing workflow detected."],"documentation":{"short":"Determines if the project is published as a package that others can easily download, install, easily update, and uninstall.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#packaging"}},{"name":"Code-Review","score":2,"reason":"Found 5/17 approved changesets -- score normalized to 2","details":null,"documentation":{"short":"Determines if the project requires human code review before pull requests (aka merge requests) are merged.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#code-review"}},{"name":"Binary-Artifacts","score":10,"reason":"no binaries found in the repo","details":null,"documentation":{"short":"Determines if the project has generated executable (binary) artifacts in the source repository.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#binary-artifacts"}},{"name":"Pinned-Dependencies","score":0,"reason":"dependency not pinned by hash detected -- score normalized to 0","details":["Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/python-package.yml:22: update your workflow using https://app.stepsecurity.io/secureworkflow/amirziai/flatten/python-package.yml/master?enable=pin","Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/python-package.yml:24: update your workflow using https://app.stepsecurity.io/secureworkflow/amirziai/flatten/python-package.yml/master?enable=pin","Warn: pipCommand not pinned by hash: .github/workflows/python-package.yml:29","Warn: pipCommand not pinned by hash: .github/workflows/python-package.yml:30","Warn: pipCommand not pinned by hash: .github/workflows/python-package.yml:31","Info:   0 out of   2 GitHub-owned GitHubAction dependencies pinned","Info:   0 out of   3 pipCommand dependencies pinned"],"documentation":{"short":"Determines if the project has declared and pinned the dependencies of its build process.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#pinned-dependencies"}},{"name":"CII-Best-Practices","score":0,"reason":"no effort to earn an OpenSSF best practices badge detected","details":null,"documentation":{"short":"Determines if the project has an OpenSSF (formerly CII) Best Practices Badge.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#cii-best-practices"}},{"name":"Fuzzing","score":0,"reason":"project is not fuzzed","details":["Warn: no fuzzer integrations found"],"documentation":{"short":"Determines if the project uses fuzzing.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#fuzzing"}},{"name":"Vulnerabilities","score":10,"reason":"0 existing vulnerabilities detected","details":null,"documentation":{"short":"Determines if the project has open, known unfixed vulnerabilities.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#vulnerabilities"}},{"name":"Security-Policy","score":0,"reason":"security policy file not detected","details":["Warn: no security policy file detected","Warn: no security file to analyze","Warn: no security file to analyze","Warn: no security file to analyze"],"documentation":{"short":"Determines if the project has published a security policy.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#security-policy"}},{"name":"License","score":10,"reason":"license file detected","details":["Info: project has a license file: LICENSE:0","Info: FSF or OSI recognized license: MIT License: LICENSE:0"],"documentation":{"short":"Determines if the project has defined a license.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#license"}},{"name":"Signed-Releases","score":-1,"reason":"no releases found","details":null,"documentation":{"short":"Determines if the project cryptographically signs release artifacts.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#signed-releases"}},{"name":"Branch-Protection","score":-1,"reason":"internal error: error during branchesHandler.setup: internal error: githubv4.Query: Resource not accessible by integration","details":null,"documentation":{"short":"Determines if the default and release branches are protected with GitHub's branch protection settings.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#branch-protection"}},{"name":"SAST","score":0,"reason":"SAST tool is not run on all commits -- score normalized to 0","details":["Warn: 0 commits out of 21 are checked with a SAST tool"],"documentation":{"short":"Determines if the project uses static code analysis.","url":"https://github.com/ossf/scorecard/blob/f6ed084d17c9236477efd66e5b258b9d4cc7b389/docs/checks.md#sast"}}]},"last_synced_at":"2025-08-16T20:25:09.442Z","repository_id":45240703,"created_at":"2025-08-16T20:25:09.442Z","updated_at":"2025-08-16T20:25:09.442Z"},"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279059631,"owners_count":26094995,"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","status":"online","status_checked_at":"2025-10-15T02:00:07.814Z","response_time":56,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["flattened-objects","flattens","json","pandas","python","unflatten"],"created_at":"2024-08-01T21:02:01.711Z","updated_at":"2025-10-15T07:41:02.684Z","avatar_url":"https://github.com/amirziai.png","language":"Python","funding_links":[],"categories":["Python"],"sub_categories":[],"readme":"[![Build Status](https://travis-ci.org/amirziai/flatten.svg?branch=master)](https://travis-ci.org/amirziai/flatten) [![PyPI version](https://badge.fury.io/py/flatten-json.svg)](https://badge.fury.io/py/flatten-json) [![Codacy Badge](https://api.codacy.com/project/badge/Coverage/7ae779ec4e99462f907c5afecfd5de48)](https://www.codacy.com/app/amirziai/flatten?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=amirziai/flatten\u0026utm_campaign=Badge_Coverage) \n\n# flatten_json\nFlattens JSON objects in Python. ```flatten_json``` flattens the hierarchy in your object which can be useful if you want to force your objects into a table.\n\n## Installation\n```bash\npip install flatten_json\n```\n\n## flatten\n\n### Usage\nLet's say you have the following object:\n```python\ndic = {\n    \"a\": 1,\n    \"b\": 2,\n    \"c\": [{\"d\": [2, 3, 4], \"e\": [{\"f\": 1, \"g\": 2}]}]\n}\n```\nwhich you want to flatten. Just apply ```flatten```:\n```python\nfrom flatten_json import flatten\nflatten(dic)\n```\n\nResults:\n```python\n{'a': 1,\n 'b': 2,\n 'c_0_d_0': 2,\n 'c_0_d_1': 3,\n 'c_0_d_2': 4,\n 'c_0_e_0_f': 1,\n 'c_0_e_0_g': 2}\n```\n\n### Usage with Pandas\nFor the following object:\n```python\ndic = [\n    {\"a\": 1, \"b\": 2, \"c\": {\"d\": 3, \"e\": 4}},\n    {\"a\": 0.5, \"c\": {\"d\": 3.2}},\n    {\"a\": 0.8, \"b\": 1.8},\n]\n```\nWe can apply `flatten` to each element in the array and then use pandas to capture the output as a dataframe:\n```python\ndic_flattened = [flatten(d) for d in dic]\n```\nwhich creates an array of flattened objects:\n```python\n[{'a': 1, 'b': 2, 'c_d': 3, 'c_e': 4},\n {'a': 0.5, 'c_d': 3.2},\n {'a': 0.8, 'b': 1.8}]\n```\nFinally you can use ```pd.DataFrame``` to capture the flattened array:\n```python\nimport pandas as pd\ndf = pd.DataFrame(dic_flattened)\n```\nThe final result as a Pandas dataframe:\n```\n\ta\tb\tc_d\tc_e\n0\t1\t2\t3\t4\n1\t0.5\tNaN\t3.2\tNaN\n2\t0.8\t1.8\tNaN\tNaN\n```\n\n### Custom separator\nBy default `_` is used to separate nested element. You can change this by passing the desired character:\n```python\nflatten({\"a\": [1]}, '|')\n```\nreturns:\n```python\n{'a|0': 1}\n```\n\n### Ignore root keys\nBy default `flatten` goes through all the keys in the object. If you are not interested in output from a set of keys you can pass this set as an argument to `root_keys_to_ignore`:\n```python\ndic = {\n    'a': {'a': [1, 2, 3]},\n    'b': {'b': 'foo', 'c': 'bar'},\n    'c': {'c': [{'foo': 5, 'bar': 6, 'baz': [1, 2, 3]}]}\n}\nflatten(dic, root_keys_to_ignore={'b', 'c'})\n```\nreturns:\n```python\n{\n    'a_a_0': 1,\n    'a_a_1': 2,\n    'a_a_2': 3\n}\n```\nThis feature can prevent unnecessary processing which is a concern with deeply nested objects.\n\n## unflatten\nReverses the flattening process. Example usage:\n```python\nfrom flatten_json import unflatten\n\ndic = {\n    'a': 1,\n    'b_a': 2,\n    'b_b': 3,\n    'c_a_b': 5\n}\nunflatten(dic)\n```\nreturns:\n```python\n{\n    'a': 1,\n    'b': {'a': 2, 'b': 3},\n    'c': {'a': {'b': 5}}\n}\n```\n\n### Unflatten with lists\n`flatten` encodes key for list values with integer indices which makes it ambiguous for reversing the process. Consider this flattened dictionary:\n```python\na = {'a': 1, 'b_0': 5}\n```\n\nBoth `{'a': 1, 'b': [5]}` and `{'a': 1, 'b': {0: 5}}` are legitimate answers.\n \nCalling `unflatten_list` the dictionary is first unflattened and then in a post-processing step the function looks for a list pattern (zero-indexed consecutive integer keys) and transforms the matched values into a list.\n \nHere's an example:\n```python\nfrom flatten_json import unflatten_list\ndic = {\n    'a': 1,\n    'b_0': 1,\n    'b_1': 2,\n    'c_a': 'a',\n    'c_b_0': 1,\n    'c_b_1': 2,\n    'c_b_2': 3\n}\nunflatten_list(dic)\n```\nreturns:\n```python\n{\n    'a': 1,\n    'b': [1, 2],\n    'c': {'a': 'a', 'b': [1, 2, 3]}\n}\n```\n\n## Command line invocation\n```bash\n\u003e\u003e\u003e echo '{\"a\": {\"b\": 1}}' | flatten_json\n{\"a_b\": 1}\n\n\u003e\u003e\u003e echo '{\"a\": {\"b\": 1}}' \u003e test.json\n\u003e\u003e\u003e cat test.json | flatten_json\n{\"a_b\": 1}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famirziai%2Fflatten","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Famirziai%2Fflatten","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Famirziai%2Fflatten/lists"}