{"id":8016,"date":"2021-01-27T14:41:44","date_gmt":"2021-01-27T09:11:44","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8016"},"modified":"2021-01-27T15:49:42","modified_gmt":"2021-01-27T10:19:42","slug":"working-with-json-in-python","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/working-with-json-in-python\/","title":{"rendered":"Working with JSON in Python"},"content":{"rendered":"\n<p>JSON has become a popular way of storing and exchanging data from the web. Whether you are <a href=\"https:\/\/www.h2kinfosys.com\/blog\/data-scraping-using-excel-vba-and-selenium\/\" class=\"rank-math-link\">scraping data <\/a>through an API or you need to store the data in some database, you will most likely be dealing with data in JSON format. And that\u2019s why in this tutorial, we will discuss how to work with JSON files using Python.\u00a0<\/p>\n\n\n\n<p>Although JSON was coined from the JavaScript programming language, need not worry. Every discussion here will utilize Python. Python provides an easy way of working with JSON files. By the end of this tutorial, you\u2019d know how to work with JSON data in Python.<\/p>\n\n\n\n<p>Specifically, here\u2019s what you\u2019d learn in this tutorial.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>What is JSON<\/li><li>The Structure and Syntax of JSON<\/li><li>The JSON Library<\/li><li>Converting a Python Object to a JSON String&nbsp;<\/li><li>Prettifying a JSON Data<\/li><li>Sorting the Keys in a JSON String<\/li><li>Saving the JSON string as a JSON file<\/li><li>Converting JSON to a Python Object<\/li><li>Decoding a JSON file to a Python Object.&nbsp;<\/li><li>Working with JSON from APIs<\/li><\/ul>\n\n\n\n<p>Let\u2019s dive in.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is JSON<\/h2>\n\n\n\n<p>JSON stands for JavaSCript Object Notation, which of course was inspired by a part of JavaScript, that deals with object literal syntax. According to their<a href=\"https:\/\/www.json.org\/json-en.html\" rel=\"nofollow noopener\" target=\"_blank\"> official website<\/a>, JSON is completely independent of any programming language. However, it uses the convention employed in the C family programming language, making it easy for C, C++, Python, C#Java programmers to easily understand. Let\u2019s understand the structure of JSON.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The Structure and Syntax of JSON<\/h2>\n\n\n\n<p>As mentioned, JSON is curled from JavaScript in the beginning so it holds its syntax for object notation.&nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Data is stored in name-value pairs<\/li><li>Objects are encapsulated in curly braces<\/li><li>Arrays are held in square brackets<\/li><li>And Data is separated by commas.&nbsp;<\/li><\/ul>\n\n\n\n<p>An example of a JSON file is shown below:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n\u2018Key_1\u2019: \u2018value_1\u2019:,\n\u2018Key_2\u2019: ;value_2\u2019,\n}\n<\/pre>\n\n\n\n<p>The datatypes permissible in JSON are:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A number<\/li><li>A string<\/li><li>A JSON object<\/li><li>A boolean<\/li><li>An array<\/li><li>Null<\/li><\/ul>\n\n\n\n<p>It is worthy of note that when moving from a file format to JSON, JSON automatically converts data types that are not listed above, to one of the above-listed data types. This is called encoding and decoding. Going forward, we will see how encoding and decoding are done between JSON and Python.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">The JSON Library<\/h2>\n\n\n\n<p>Python comes with an inbuilt library for working with JSON files, the <a href=\"https:\/\/www.json.org\/json-en.html\" class=\"rank-math-link\" rel=\"nofollow noopener\" target=\"_blank\">JSON library<\/a>. You can access this library with a simple import statement.<\/p>\n\n\n\n<p><code>import json<\/code><\/p>\n\n\n\n<p>Using this library, here are the 4 most common methods to use when working with JSON files.&nbsp;<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>dumps() method: This is used for converting a Python object into a JSON string (encoding)<\/li><li>dump() method: This is used for converting a Python object into a JSON file (encoding)<\/li><li>loads() method: This is used for converting a JSON string to a Python object (decoding)<\/li><li>load() method This is used for converting a JSON file to a Python object (decoding)<\/li><\/ol>\n\n\n\n<p>Let\u2019s take each of these processes one after the other.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Converting a Python Object to a JSON String&nbsp;<\/h2>\n\n\n\n<p>As earlier mentioned, JSON automatically changes data types during the encoding process. When encoding (converting Python to JSON), here\u2019s how the data types are converted.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Python<\/strong><\/td><td><strong>JSON<\/strong><\/td><\/tr><tr><td>list<\/td><td>Array<\/td><\/tr><tr><td>Dict<\/td><td>Object<\/td><\/tr><tr><td>Float<\/td><td>Real number<\/td><\/tr><tr><td>unicode<\/td><td>String<\/td><\/tr><tr><td>None<\/td><td>null<\/td><\/tr><tr><td>True<\/td><td>True<\/td><\/tr><tr><td>False<\/td><td>False<\/td><\/tr><tr><td>Int, long<\/td><td>int<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p>Now, let\u2019s go ahead and convert a Python object to a JSON string. Recall we use the dumps() method.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\n#import necessary libraries\nimport json\n#define a python object as a dictionary\npython_object = {\n\u00a0\u00a0\u00a0\u00a0\"id\": \"0001\",\n\u00a0\u00a0\u00a0\u00a0\"type\": None,\n\u00a0\u00a0\u00a0\u00a0\"name\": \"Cake\",\n\u00a0\u00a0\u00a0\u00a0\"image\":\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/0001.jpg\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 200,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 200\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\n\u00a0\u00a0\u00a0\u00a0\"thumbnail\":\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/thumbnails\/0001.jpg\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 32.3,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 32.5\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n}\n#encode string\njson_string = json.dumps(python_string)\n#print json string\nprint(json_string)<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\"id\": \"0001\", \"type\": null, \"name\": \"Cake\", \"image\": {\"url\": \"images\/0001.jpg\", \"width\": 200, \"height\": 200}, \"thumbnail\": {\"url\": \"images\/thumbnails\/0001.jpg\", \"width\": 32.3, \"height\": 32.5}}\u00a0<\/pre>\n\n\n\n<p>You\u2019d notice some of the data type changes. For instance, the None data type is now changed to null.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prettifying a JSON Data<\/h2>\n\n\n\n<p>To beautify our JSON string a bit, we can add some indentation to the JSON string. This is done by defining the indent argument. Let\u2019s set the indent to 2 and observe the result.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#encode string\njson_string = json.dumps(python_object, indent=3)<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n\u00a0\u00a0\u00a0\"id\": \"0001\",\n\u00a0\u00a0\u00a0\"type\": null,\n\u00a0\u00a0\u00a0\"name\": \"Cake\",\n\u00a0\u00a0\u00a0\"image\": {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/0001.jpg\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 200,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 200\n\u00a0\u00a0\u00a0},\n\u00a0\u00a0\u00a0\"thumbnail\": {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/thumbnails\/0001.jpg\",\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 32.3,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 32.5\n\u00a0\u00a0\u00a0}\n}\n<\/pre>\n\n\n\n<p>Now, you can read and understand the file better. You\u2019d observe image and thumbnail objects are another object with keys and values<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Sorting the Keys in a JSON String<\/h2>\n\n\n\n<p>If you wish, you can sort the keys in the JSON String using the sort_keys argument. When you set it to True, the keys are sorted in alphabetical order. Let\u2019s try it out.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#encode string with sorted keys\njson_string = json.dumps(python_object, indent=3, sort_keys=True)<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{\n\u00a0\u00a0\u00a0\"id\": \"0001\",\n\u00a0\u00a0\u00a0\"image\": {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 200,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/0001.jpg\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 200\n\u00a0\u00a0\u00a0},\n\u00a0\u00a0\u00a0\"name\": \"Cake\",\n\u00a0\u00a0\u00a0\"thumbnail\": {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 32.5,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/thumbnails\/0001.jpg\",\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 32.3\n\u00a0\u00a0\u00a0},\n\u00a0\u00a0\u00a0\"type\": null\n}\n<\/pre>\n\n\n\n<p>You\u2019d see that the keys are now sorted in alphabetical order: id, image, name, thumbnail, and type.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Saving the JSON string as a JSON file<\/h2>\n\n\n\n<p>You can decide to save the JSON string as a file. This time, you use the dump() method rather than the dumps() method.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s see an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\n#import necessary libraries\nimport json\n#define a python object as a dictionary\npython_object = {\n\u00a0\u00a0\u00a0\u00a0\"id\": \"0001\",\n\u00a0\u00a0\u00a0\u00a0\"type\": None,\n\u00a0\u00a0\u00a0\u00a0\"name\": \"Cake\",\n\u00a0\u00a0\u00a0\u00a0\"image\":\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/0001.jpg\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 200,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 200\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0},\n\u00a0\u00a0\u00a0\u00a0\"thumbnail\":\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/thumbnails\/0001.jpg\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 32.3,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 32.5\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\n}\n#encode string with sorted keys\nwith open('new_jsonfile.json', 'w') as file:\n\u00a0\u00a0\u00a0\u00a0json.dump(python_object, file, indent=3, sort_keys=True)\n\u00a0\n<\/pre>\n\n\n\n<p>Observe that the dump method takes the argument of the python object to encode and the file to be dumped into.<\/p>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/0R2Kbr-tb_4yiWtDXbb9wQgGO_mt_KhlR-izfkH3qnAx00LALGBP5dMQrRS5ayWqwDABPxcidMxybflslPZWWvLOrxVVD5Wg0D-JNAsISvGaBskCmaVAgt5sP3L-eRomZHmjsl8\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>A new JSON file has been created.&nbsp;<\/p>\n\n\n\n<p>Next, let\u2019s see how to decode a JSON file.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Converting JSON to a Python Object<\/h2>\n\n\n\n<p>Changing a JSON to a Python object is called decoding. In this section, we will discuss how to decode a JSON string or file. Let\u2019s begin with a JSON string. To decode a JSON string, you use the loads() method.&nbsp;<\/p>\n\n\n\n<p>Let\u2019s see an example.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\n#import necessary libraries\nimport json\n#define a json string as a dictionary\njson_string = '''\n{\n\u00a0\u00a0\u00a0\"id\": \"0001\",\n\u00a0\u00a0\u00a0\"type\": null,\n\u00a0\u00a0\u00a0\"name\": \"Cake\",\n\u00a0\u00a0\u00a0\"image\": {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/0001.jpg\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 200,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 200\n\u00a0\u00a0\u00a0},\n\u00a0\u00a0\u00a0\"thumbnail\": {\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"url\": \"images\/thumbnails\/0001.jpg\",\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"width\": 32.3,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"height\": 32.5\n\u00a0\u00a0\u00a0}\n}\n\u00a0\n'''\n#decode string with sorted keys\npython_object = json.loads(json_string)\n#print the result\nprint(python_object)\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{'id': '0001', 'type': None, 'name': 'Cake', 'image': {'url': 'images\/0001.jpg', 'width': 200, 'height': 200}, 'thumbnail': {'url': 'images\/thumbnails\/0001.jpg', 'width': 32.3, 'height': 32.5}}\u00a0<\/pre>\n\n\n\n<p>Notice that the null datatype has been converted to a None data type. We can return the values of the <a href=\"https:\/\/www.h2kinfosys.com\/blog\/how-to-print-objects-in-python-using-the-print-function\/\" class=\"rank-math-link\">Python object <\/a>as a dictionary. Say we wish to check the values of the thumbnail key, we can write<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#print the values of the thumbnail key\nprint(python_object['thumbnail'])\n<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{'url': 'images\/thumbnails\/0001.jpg', 'width': 32.3, 'height': 32.5}<\/pre>\n\n\n\n<p>You can as well parse a JSON file and convert it to a Python object.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Decoding a JSON file to a Python Object.&nbsp;<\/h2>\n\n\n\n<p>To decode a JSON file to a python object, you use the load() method rather than the loads() method.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import necessary libraries\nimport json\n#read json file\nwith open('new_jsonfile.json') as file:\n\u00a0\u00a0\u00a0\u00a0new_python_object = json.load(file)\n\u00a0\n#print the result\nprint(new_python_object)<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">{'id': '0001', 'image': {'height': 200, 'url': 'images\/0001.jpg', 'width': 200}, 'name': 'Cake', 'thumbnail': {'height': 32.5, 'url': 'images\/thumbnails\/0001.jpg', 'width': 32.3}, 'type': None}\u00a0<\/pre>\n\n\n\n<p>As seen, the JSON file we created the last time is decoded and printed as a python dictionary<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working with JSON from APIs<\/h2>\n\n\n\n<p>When scraping data from the web using an API, most APIs return a JSON file. This is one common application of working with JSON files. Let\u2019s say we wish to scrap the exchange information for different currencies, we can get the data from GDAX API. See the example below.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the necessary libraries\nimport json\nfrom urllib.request import urlopen\n\u00a0\n#open the JSON from the API\nwith urlopen('https:\/\/api.gdax.com\/products\/') as response:\n\u00a0\u00a0\u00a0\u00a0texts = response.read()\n\u00a0\n#convert json to python objects\ndata = json.loads(texts)\n\u00a0\n#convert python object to a prettified json\nprint(json.dumps(data, indent=3))<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"id\": \"LOOM-USDC\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_currency\": \"LOOM\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"quote_currency\": \"USDC\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_min_size\": \"1.00000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_max_size\": \"2500000.00000000\",\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"quote_increment\": \"0.00000100\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_increment\": \"1.00000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"display_name\": \"LOOM\/USDC\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"min_market_funds\": \"0.1\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"max_market_funds\": \"100000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"margin_enabled\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"post_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"limit_only\": true,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"cancel_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"trading_disabled\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"status\": \"online\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"status_message\": \"\"\n\u00a0\u00a0\u00a0},\n\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"id\": \"DAI-USDC\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_currency\": \"DAI\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"quote_currency\": \"USDC\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_min_size\": \"1.00000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_max_size\": \"100000.00000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"quote_increment\": \"0.00000100\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_increment\": \"0.00001000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"display_name\": \"DAI\/USDC\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"min_market_funds\": \"5\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"max_market_funds\": \"100000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"margin_enabled\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"post_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"limit_only\": true,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"cancel_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"trading_disabled\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"status\": \"online\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"status_message\": \"\"\n\u00a0\u00a0\u00a0},\n\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"id\": \"XTZ-USD\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_currency\": \"XTZ\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"quote_currency\": \"USD\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_min_size\": \"1.00000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_max_size\": \"100000.00000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"quote_increment\": \"0.00010000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_increment\": \"0.01000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"display_name\": \"XTZ\/USD\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"min_market_funds\": \"10\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"max_market_funds\": \"100000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"margin_enabled\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"post_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"limit_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"cancel_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"trading_disabled\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"status\": \"online\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"status_message\": \"\"\n\u00a0\u00a0\u00a0},\n\u00a0\u00a0\u00a0{\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"id\": \"EOS-BTC\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_currency\": \"EOS\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"quote_currency\": \"BTC\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_min_size\": \"0.10000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_max_size\": \"50000.00000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"quote_increment\": \"0.00000100\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"base_increment\": \"0.10000000\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"display_name\": \"EOS\/BTC\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"min_market_funds\": \"0.001\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"max_market_funds\": \"30\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"margin_enabled\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"post_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"limit_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"cancel_only\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"trading_disabled\": false,\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"status\": \"online\",\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"status_message\": \"\"\n\u00a0\u00a0\u00a0},\n}\n<\/pre>\n\n\n\n<p>There was more to this output by the way. If say, we want to print all the currency exchanges in this data, we can write<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#print the list of exchanges available\nfor item in data:\n\u00a0\u00a0\u00a0\u00a0print(item['id'], end=', ')\nprint()\u00a0\u00a0\u00a0\u00a0\nprint(f'There are {len(data)} currency exchanges in this data')<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ETC-GBP, FIL-USD, BNT-GBP, ETH-EUR, NMR-BTC, XRP-USD, UNI-BTC, FIL-EUR, BNT-BTC, AAVE-GBP, BNT-EUR,\u00a0\nBTC-GBP, BCH-BTC, LRC-USD, XRP-EUR, EOS-USD, ALGO-GBP, EOS-EUR, MKR-USD, UNI-USD, UMA-EUR, COMP-BTC, BNT-USD, ETH-USDC, NU-GBP, NU-EUR, LTC-BTC, REP-USD, BAND-BTC, EOS-BTC, KNC-USD, LTC-USD, DASH-USD, NU-USD, WBTC-USD, BTC-USDC, ZEC-USD, XLM-USD, XTZ-USD, FIL-BTC, BAL-USD, ETH-BTC, SNX-EUR, SNX-GBP, YFI-BTC, DASH-BTC, DAI-USD, GRT-BTC, UMA-GBP, CVC-USDC, XTZ-BTC, REN-BTC, ZEC-USDC, GNT-USDC, CGLD-EUR, LTC-EUR, FIL-GBP, CGLD-USD, ALGO-BTC, XTZ-EUR, GRT-USD, MANA-USDC, SNX-BTC, ATOM-USD, BAL-BTC,\u00a0\nKNC-BTC, CGLD-BTC, NMR-USD, BAND-EUR, ALGO-EUR, OMG-EUR, XLM-BTC, ETC-USD, YFI-USD, BAT-USDC, OMG-BTC, LINK-EUR, NU-BTC, LINK-ETH, OMG-USD, ETH-GBP, DNT-USDC, ZRX-EUR, REP-BTC, CGLD-GBP, AAVE-EUR, ETC-EUR, REN-USD, UMA-USD, LRC-BTC, XRP-GBP, XTZ-GBP, ETC-BTC, ATOM-BTC, NMR-GBP, LOOM-USDC, ZEC-BTC, BCH-GBP, GRT-GBP, COMP-USD, ETH-USD, BCH-USD, ETH-DAI, BCH-EUR, AAVE-USD, UMA-BTC, OXT-USD, BAND-GBP, BAT-ETH, XRP-BTC, XLM-EUR, MKR-BTC, WBTC-BTC, ALGO-USD, SNX-USD, DAI-USDC, OMG-GBP, LINK-BTC, LINK-USD, BTC-EUR, ZRX-BTC, AAVE-BTC, LTC-GBP, GRT-EUR, BAND-USD, NMR-EUR, ZRX-USD, BTC-USD, LINK-GBP, There are 129 currency exchanges in this data\n<\/pre>\n\n\n\n<p>If say we wish to check the currency exchange with the highest market fund, we can run<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#create a list to store max market fund\nmax_market_list = []\nfor item in data:\n\u00a0\u00a0\u00a0\u00a0max_market_list.append((item['id'], item['max_market_funds']))\n#print the max market fund currency and it's value\nprint(max(max_market_list))<\/pre>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<p><code>('ZRX-USD', '100000')<\/code><\/p>\n\n\n\n<p>As seen, the ZRX cryptocurrency to the USD has the highest market fund.&nbsp;<\/p>\n\n\n\n<p>So basically, this is how you can play around with data in JSON format from APIs.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">To conclude,&nbsp;<\/h2>\n\n\n\n<p>We have discussed how to encode and decode JSON strings and files in Python. You have also learned that it is good practice to prettify your JSON files using the indent argument. Finally, you have seen how to work with JSON files from APIs.&nbsp;<\/p>\n\n\n\n<p>If you\u2019ve got any questions, feel free to leave them in the comment section and I\u2019d do my best to answer them.&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>JSON has become a popular way of storing and exchanging data from the web. Whether you are scraping data through an API or you need to store the data in some database, you will most likely be dealing with data in JSON format. And that\u2019s why in this tutorial, we will discuss how to work [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8031,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8016","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8016","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8016"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8016\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/8031"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8016"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8016"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8016"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}