{"id":20710031,"url":"https://github.com/oxylabs/python-parse-json","last_synced_at":"2025-04-23T04:48:18.338Z","repository":{"id":52154323,"uuid":"469655586","full_name":"oxylabs/python-parse-json","owner":"oxylabs","description":"A tutorial for parsing JSON data with Python","archived":false,"fork":false,"pushed_at":"2025-02-11T12:48:41.000Z","size":21,"stargazers_count":2,"open_issues_count":0,"forks_count":4,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-03-29T22:12:05.977Z","etag":null,"topics":["github-python","javascript","json","json-api","json-data","json-database-python","json-parser","parse","parser","python","python-ecommerce","python-web-crawler","scraper-python","serp-api-python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/oxylabs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2022-03-14T09:05:45.000Z","updated_at":"2025-02-11T12:48:45.000Z","dependencies_parsed_at":"2024-04-19T11:46:21.228Z","dependency_job_id":"c0b172e4-665b-4a1d-8990-ef02702686b5","html_url":"https://github.com/oxylabs/python-parse-json","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oxylabs%2Fpython-parse-json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oxylabs%2Fpython-parse-json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oxylabs%2Fpython-parse-json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/oxylabs%2Fpython-parse-json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/oxylabs","download_url":"https://codeload.github.com/oxylabs/python-parse-json/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250372939,"owners_count":21419722,"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":["github-python","javascript","json","json-api","json-data","json-database-python","json-parser","parse","parser","python","python-ecommerce","python-web-crawler","scraper-python","serp-api-python"],"created_at":"2024-11-17T02:09:35.493Z","updated_at":"2025-04-23T04:48:18.332Z","avatar_url":"https://github.com/oxylabs.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Reading \u0026 Parsing JSON Data With Python\n\n[![Oxylabs promo code](https://raw.githubusercontent.com/oxylabs/product-integrations/refs/heads/master/Affiliate-Universal-1090x275.png)](https://oxylabs.go2cloud.org/aff_c?offer_id=7\u0026aff_id=877\u0026url_id=112)\n\n[![](https://dcbadge.vercel.app/api/server/eWsVUJrnG5)](https://discord.gg/GbxmdGhZjq)\n\n[\u003cimg src=\"https://img.shields.io/static/v1?label=\u0026message=Json\u0026color=brightgreen\" /\u003e](https://github.com/topics/json) [\u003cimg src=\"https://img.shields.io/static/v1?label=\u0026message=Python\u0026color=important\" /\u003e](https://github.com/topics/python)\n\n\n- [What is JSON?](#what-is-json)\n- [Converting JSON string to Python object](#converting-json-string-to-python-object)\n- [Converting JSON file to Python object](#converting-json-file-to-python-object)\n- [Converting Python object to JSON string](#converting-python-object-to-json-string)\n- [Writing Python object to a JSON file](#writing-python-object-to-a-json-file)\n- [Converting custom Python objects to JSON objects](#converting-custom-python-objects-to-json-objects)\n- [Creating Python class objects from JSON objects](#creating-python-class-objects-from-json-objects)\n\n\nJSON is a common standard used by websites and APIs and even natively supported by modern databases such as PostgreSQL. In this article, we’ll present a tutorial on how to handle JSON data with Python\n\nFor a detailed explanation, see our [blog post](https://oxy.yt/9rL7).\n\n## What is JSON?\n\nJSON, or JavaScript Object Notation, is a format that uses text to store data objects:\n\n```json\n{\n   \"name\": \"United States\",\n   \"population\": 331002651,\n   \"capital\": \"Washington D.C.\",\n   \"languages\": [\n      \"English\",\n      \"Spanish\"\n   ]\n}\n```\n\n## Converting JSON string to Python object\n\nLet’s start with a simple example:\n\n```python\n# JSON string\ncountry = '{\"name\": \"United States\", \"population\": 331002651}'\nprint(type(country))\n```\n\nThe output of this snippet will confirm that this is indeed a string:\n\n```python\n\u003cclass 'str'\u003e\n```\n\nWe can call the `json.loads()` method and provide this string as a parameter.\n\n```python\nimport json\n\ncountry = '{\"name\": \"United States\", \"population\": 331002651}'\ncountry_dict = json.loads(country)\n\nprint(type(country))\nprint(type(country_dict))\n```\n\nThe output of this snippet will confirm that the JSON data, which was a string, is now a Python dictionary.\n\n```python\n\u003cclass 'str'\u003e\n\u003cclass 'dict'\u003e\n```\n\nThis dictionary can be accessed as usual:\n\n```python\nprint(country_dict['name'])\n# OUTPUT:   United States\n```\n\nIt is important to note here that the `json.loads()` method will not always return a dictionary. The data type that is returned will depend on the input string. For example, this JSON string will return a list, not a dictionary.\n\n```\ncountries = '[\"United States\", \"Canada\"]'\ncounties_list= json.loads(countries)\n\nprint(type(counties_list))\n# OUTPUT:  \u003cclass 'list'\u003e\n```\n\nSimilarly, if the JSON string contains `true`, it will be converted to Python equivalent boolean value, which is `True`.\n\n```\nimport json\n \nbool_string = 'true'\nbool_type = json.loads(bool_string)\nprint(bool_type)\n# OUTPUT:  True\n```\n\nThe following table shows JSON objects and the Python data types after conversion. For more details, see [Python docs](https://docs.python.org/3/library/json.html%23json-to-py-table).\n\n## Converting JSON file to Python object\n\nSave the following JSON data as a new file and name it `united_states.json`:\n\n```json\n{\n   \"name\": \"United States\",\n   \"population\": 331002651,\n   \"capital\": \"Washington D.C.\",\n   \"languages\": [\n      \"English\",\n      \"Spanish\"\n   ]\n}\n```\n\nEnter this Python script in a new file:\n\n```python\nimport json\n\nwith open('united_states.json') as f:\n  data = json.load(f)\n\nprint(type(data))\n```\n\nRunning this Python file prints the following:\n\n```py'\n\u003cclass 'dict'\u003e\n```\n\nThe dictionary keys can be checked as follows:\n\n```py'\nprint(data.keys())\n# OUTPUT:  dict_keys(['name', 'population', 'capital', 'languages'])\n```\n\nUsing this information, the value of `name` can be printed as follows:\n\n```python\ndata['name']\n# OUTPUT:  United States\n```\n\n## Converting Python object to JSON string\n\nSave this code in a new file as a Python script:\n\n```python\nimport json\n\nlanguages = [\"English\",\"French\"]\ncountry = {\n    \"name\": \"Canada\",\n    \"population\": 37742154,\n    \"languages\": languages,\n    \"president\": None,\n}\n\ncountry_string = json.dumps(country)\nprint(country_string)\n```\n\nWhen this file is run with Python, the following output is printed:\n\n```python\n{\"name\": \"Canada\", \"population\": 37742154, \"languages\": [\"English\", \"French\"],\n \"president\": null}\n```\n\nLists can be converted to JSON as well. Here is the Python script and its output:\n\n```python\nimport json\n\nlanguages = [\"English\", \"French\"]\n\nlanguages_string = json.dumps(languages)\nprint(languages_string)\n# OUTPUT:   [\"English\", \"French\"]\n```\n\nIt’s not just limited to a dictionary and a list. `string`, `int`, `float`, `bool` and even `None` value can be converted to JSON. \n\n## Writing Python object to a JSON file\n\nThe method used to write a JSON file is `dump()`:\n\n```python\nimport json\n\n# Tuple is encoded to JSON array.\nlanguages = (\"English\", \"French\")\n# Dictionary is encoded to JSON object.\ncountry = {\n    \"name\": \"Canada\",\n    \"population\": 37742154,\n    \"languages\": languages,\n    \"president\": None,\n}\n\nwith open('countries_exported.json', 'w') as f:\n    json.dump(country, f)\n```\n\n To make it more readable, we can pass one more parameter to the `dump()` function as follows:\n\n```python\njson.dump(country, f, indent=4)\n```\n\nThis time when you run the code, it will be nicely formatted with indentation of 4 spaces:\n\n```json\n{\n    \"languages\": [\n        \"English\", \n        \"French\"\n    ], \n    \"president\": null, \n    \"name\": \"Canada\", \n    \"population\": 37742154\n}\n```\n\n## Converting custom Python objects to JSON objects\n\nSave the following code as a Python script and run it:\n\n```python\nimport json\n\nclass Country:\n    def __init__(self, name, population, languages):\n        self.name = name    \n        self.population = population\n        self.languages = languages\n\n    \ncanada = Country(\"Canada\", 37742154, [\"English\", \"French\"])\n\nprint(json.dumps(canada))\n# OUTPUT:   TypeError: Object of type Country is not JSON serializable\n```\n\nTo convert the objects to JSON, we need to write a new class that extends JSONEncoder:\n\n```python\nimport json \n \nclass CountryEncoder(json.JSONEncoder):\n    def default(self, o): \n        if isinstance(o, Country):\n           # JSON object would be a dictionary.\n\t\t\t\t\t\treturn {\n                \"name\" : o.name,\n                \"population\": o.population,\n                \"languages\": o.languages\n            } \n        else:\n            # Base class will raise the TypeError.\n            return super().default(o)\n```\n\nThis class can now be supplied to the `json.dump()` as well as `json.dumps()` methods.\n\n```python\nprint(json.dumps(canada, cls=CountryEncoder))\n# OUTPUT:  {“name\": \"Canada\", \"population\": 37742154, \"languages\": [\"English\", \"French\"]}\n```\n\n## Creating Python class objects from JSON objects\n\nUsing a custom encoder, we were able to write code like this:\n\n```python\n# Create an object of class Country\ncanada = Country(\"Canada\", 37742154, [\"English\", \"French\"])\n# Use json.dump() to create a JSON file in writing mode\nwith open('canada.json','w') as f:\n    json.dump(canada,f, cls=CountryEncoder)\n```\n\nIf we try to parse this JSON file using the `json.load()` method, we will get a dictionary:\n\n```python\nwith open('canada.json','r') as f:\n    country_object = json.load(f)\n# OUTPUT:  \u003ctype ‘dict'\u003e\n```\n\nTo get an instance of the `Country` class instead of a dictionary, we need to create a custom decoder:\n\n```python\nimport json\n \nclass CountryDecoder(json.JSONDecoder):\n    def __init__(self, object_hook=None, *args, **kwargs):\n        super().__init__(object_hook=self.object_hook, *args, **kwargs)\n\n    def object_hook(self, o):\n        decoded_country =  Country(\n            o.get('name'), \n            o.get('population'), \n            o.get('languages'),\n        )\n        return decoded_country\n```\n\nFinally, we can call the `json.load()` method and set the `cls` parameter to `CountryDecoder` class.\n\n```python\nwith open('canada.json','r') as f:\n    country_object = json.load(f, cls=CountryDecoder)\n\nprint(type(country_object))\n# OUTPUT:  \u003cclass ‘Country'\u003e\n```\n\n\nIf you wish to find out more about Reading \u0026 Parsing JSON Data With Python, see our [blog post](https://oxy.yt/9rL7).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foxylabs%2Fpython-parse-json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foxylabs%2Fpython-parse-json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foxylabs%2Fpython-parse-json/lists"}