I'm trying to convert a JSON file to CSV as structured below:
I'm using the json and csv modules and my current solution reads the json file, spits out the unique headers (a, b, c, d, ...) Matches a slot to a header But does not write the csv yet.
My main issue is trying to convert something like: inventory,a,b,... slot1 a,b,... to 0 and 1 values if the entity contains it
There's also a lot of for loops involved and I'm not sure that is best practice: Nested for loop for getting all the headers, Looping through the json file, Writing csv file
tl;dr problem is: translate list of "inventory items" from JSON per slot, to 0s and 1s in a CSV I'd also like it to work for growing inventories (maybe e,f,g,h,...) more than 4 slots, etc So I'm trying to avoid any hard-coded rows and columns
First time using stackoverflow, thank you!
equip_inventory.json
{
"slot1": ["a", "b"],
"slot2": ["c", "d"],
"slot3": ["a", "b", "c"]
"slot4": ["d"]
}
Intended output CSV
inventory,a,b,c,d, ...
slot1,1,1,0,0,...
slot2,0,0,1,1,...
slot3,1,1,1,0,...
slot4,0,0,0,1,..