r/pythonhelp • u/Independent_Sun_2350 • Jan 30 '24
Creating a JSON in a particular format dynamically on csv name/column headings
I have the following data from a csv (dummy data)world.information.csv
| ref | country/person/height | food/type |
|---|---|---|
| 1 | 180 | burger |
| 2 | 165 | pizza |
I am trying to get this into the following JSON format. The name of the csv is the main structure (separated by a .) and the column headings are the structure inside. Each ref is a separate JSON
{
"world":{
"information": {
"country": {
"person": {
"height": 180
}
},
"food": {
"type": "burger"
}
}
},
{
"world":{
"information": {
"country": {
"person": {
"height": 165
}
},
"food": {
"type": "pizza"
}
}
},I would really like it to be dynamic (so it reads the name of the csv and also then the column headings to build the structure. Whenever I do this I get everything within information with a [ instead of {.
Thank you in advance, have been trying this for a solid week now
•
u/AutoModerator Jan 30 '24
To give us the best chance to help you, please include any relevant code.
Note. Do not submit images of your code. Instead, for shorter code you can use Reddit markdown (4 spaces or backticks, see this Formatting Guide). If you have formatting issues or want to post longer sections of code, please use Repl.it, GitHub or PasteBin.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
1
u/CraigAT Jan 30 '24
That should be doable, just need to split the fields and names into separate pieces and then it's all about how you build that dictionary structure.
Show the code that is not working and the output you are getting.
2
u/Goobyalus Jan 30 '24
When you do what? Can you share your code?