Python to convert json to yaml
import json
import yaml
# Convert the JSON response to a Python dictionary
response_dict = json.loads(json.dumps(response))
# Convert the Python dictionary to YAML (if desired)
response_yaml = yaml.dump(response_dict, default_flow_style=False)
print(response_yaml)
#####################################
import json
# Define JSON data as a Python dictionary
json_data = {
"books": [
{
"title": "The Great Gatsby",
"author": "F. Scott Fitzgerald",
"publication_year": 1925,
"genre": "Fiction",
"isbn": "978-0743273565"
},
{
"title": "To Kill a Mockingbird",
"author": "Harper Lee",
"publication_year": 1960,
"genre": "Fiction",
"isbn": "978-0061120084"
}
]
}
# Access and print the author and genre for each book
for book in json_data['books']:
author = book['author']
genre = book['genre']
print(f"Author: {author}, Genre: {genre}")
Comments
Post a Comment