{"id":26715,"date":"2022-04-01T00:19:26","date_gmt":"2022-03-31T18:49:26","guid":{"rendered":"https:\/\/python-programs.com\/?p=26715"},"modified":"2022-04-01T00:19:26","modified_gmt":"2022-03-31T18:49:26","slug":"how-to-print-particular-json-value-in-python","status":"publish","type":"post","link":"https:\/\/python-programs.com\/how-to-print-particular-json-value-in-python\/","title":{"rendered":"How to Print Particular JSON Value in Python?"},"content":{"rendered":"
JSON is an abbreviation for JavaScript Object Notation. It means that data is stored and transferred using a script (executable) file comprised of text in a computer language. Python has a built-in library named json that handles JSON. In order to use this feature, we must first load the json package into our Python script. The text in JSON is represented by a quoted-string, which contains the value in the key-value mapping within flower braces { }.<\/p>\n
How to Read from JSON?<\/strong><\/p>\n In Python, loading a JSON object is simple. Python includes a library named json that may be used to work with JSON data. It is accomplished by utilizing the JSON module, which gives us a plethora of methods, among which the loads() and load() methods will assist us in reading the JSON file.<\/p>\n Here we take\u00a0demo.json<\/strong> as an Example<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n JSON is an abbreviation for JavaScript Object Notation. It means that data is stored and transferred using a script (executable) file comprised of text in a computer language. Python has a built-in library named json that handles JSON. In order to use this feature, we must first load the json package into our Python script. …<\/p>\n{\r\n\"id\": 1,\r\n \"name\":\u00a0\"virat\",\r\n \"Address\":\u00a0[{\r\n \u00a0\u00a0\"city\"\u00a0:\u00a0\"hyderabad\",\r\n \u00a0\u00a0\"state\":\u00a0\"telangana\",\r\n \u00a0\u00a0\"geo\":\u00a0{\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"lat\":\u00a090,\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\"long\":\u00a0100\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}]\r\n\u00a0}<\/pre>\n<\/div>\n<\/div>\n
Printing Particular JSON Value in Python<\/h2>\n
\n
# Import json module using the import keyword\r\nimport json\r\n# Open some random json file using the open() function by passing the\r\n# filename\/path as an argument to it and store it in a variable\r\ngvn_jsonfile = open(\"demo.json\")\r\n# Load\/get the content of the given json file using the load() function of the\r\n# json module and store it in another variable\r\njson_data = json.load(gvn_jsonfile)\r\n# Print the data of the given JSON file\r\nprint(\"The data of the given JSON file:\\n\", json_data)\r\nprint()\r\n# Get a specific value from the json file using slicing\r\n# Printing Address details from a given JSON file\r\nprint(\"Printing Address details:\")\r\nprint(json_data[\"Address\"])\r\nprint()\r\n# Printing 'geo' details in Address\r\nprint(\"Printing 'geo' details in Address:\")\r\nprint(json_data[\"Address\"][0][\"geo\"])\r\nprint()\r\n# Printing 'lat' details in Address\/geo\r\nprint(\"Printing 'lat' details in Address\/geo:\")\r\nprint(json_data[\"Address\"][0][\"geo\"][\"lat\"])\r\n<\/pre>\n
The data of the given JSON file:\r\n{'id': 1, 'name': 'virat', 'Address': [{'city': 'hyderabad', 'state': 'telangana', 'geo': {'lat': 90, 'long': 100}}]}\r\n\r\nPrinting Address details:\r\n[{'city': 'hyderabad', 'state': 'telangana', 'geo': {'lat': 90, 'long': 100}}]\r\n\r\nPrinting 'geo' details in Address:\r\n{'lat': 90, 'long': 100}\r\n\r\nPrinting 'lat' details in Address\/geo:\r\n90<\/pre>\n","protected":false},"excerpt":{"rendered":"