
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to convert JSON data into a Python tuple?
You can first convert the json to a dict using json.loads and then convert it to a python tuple using dict.items(). You can parse JSON files using the json module in Python. This module parses the json and puts it in a dict. You can then get the values from this like a normal dict. For example, if you have a json with the following content −
Example
{ "id": "file", "value": "File", "popup": { "menuitem": [ {"value": "New", "onclick": "CreateNewDoc()"}, {"value": "Open", "onclick": "OpenDoc()"}, {"value": "Close", "onclick": "CloseDoc()"} ] } }
You can load it in your python program and loop over its keys in the following way −
import json f = open('data.json') data = json.load(f) f.close() print(tuple(data.items()))
Output
This will give the output −
(('id', 'file'), ('value', 'File'), ('popup', {'menuitem': [{'value': 'New', 'onclick': 'CreateNewDoc()'}, {'value': 'Open', 'onclick': 'OpenDoc()'}, {'value': 'Close', 'onclick': 'CloseDoc()'}]}))
- Related Articles
- How to convert JSON data into a Python object?
- How to convert a list into a tuple in Python?
- How to convert python tuple into a two-dimensional table?
- Python program to convert Set into Tuple and Tuple into Set
- How to convert pandas DataFrame into JSON in Python?
- How I can convert a Python Tuple into Dictionary?
- How can I convert Python strings into tuple?
- How we can convert Python objects into JSON objects?
- Convert a list into tuple of lists in Python
- How to convert a JSON string into a JavaScript object?
- How can I convert a bytes array into JSON format in Python?
- How to convert JSON string into Lua table?
- How to convert JSON results into a date using JavaScript?
- How to convert a tuple into an array in C#?
- How can I convert a Python tuple to string?

Advertisements