Skip to content

Instantly share code, notes, and snippets.

@rg3915
Created December 9, 2022 05:47
Show Gist options
  • Save rg3915/8e1f4174346d5cd437431b6603fbb9be to your computer and use it in GitHub Desktop.
Save rg3915/8e1f4174346d5cd437431b6603fbb9be to your computer and use it in GitHub Desktop.
yaml to json
{
"data": [
{
"id": 1,
"name": {
"first_name": "Keanu",
"last_name": "Reeves"
},
"email": "[email protected]",
"movies": [
"Matrix",
"John Week",
"Constantine"
]
},
{
"id": 2,
"name": {
"first_name": "John",
"last_name": "Travolta"
},
"email": "[email protected]",
"movies": [
"Saturday Night",
"Pulp Fiction",
"Swordfish"
]
}
]
}
data:
- id: 1
name:
first_name: Keanu
last_name: Reeves
email: [email protected]
movies:
- Matrix
- John Week
- Constantine
- id: 2
name:
first_name: John
last_name: Travolta
email: [email protected]
movies:
- Saturday Night
- Pulp Fiction
- Swordfish
import yaml
import json
with open('example.yml', 'r') as file:
configuration = yaml.safe_load(file)
with open('example.json', 'w') as json_file:
json.dump(configuration, json_file, indent=2)
output = json.dumps(json.load(open('example.json')), indent=2)
print(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment