I want to convert a dataframe from this format into JSON
Dataframe Table
Region | Province | Municipality | Barangay |
---|---|---|---|
Region 1 | Province 1 | Municipality 1 | Barangay 1 |
Region 1 | Province 1 | Municipality 1 | Barangay 2 |
Region 1 | Province 1 | Municipality 2 | Barangay 3 |
Region 1 | Province 1 | Municipality 2 | Barangay 4 |
Region 1 | Province 2 | Municipality 3 | Barangay 5 |
Region 1 | Province 2 | Municipality 3 | Barangay 6 |
Region 1 | Province 2 | Municipality 4 | Barangay 7 |
Region 1 | Province 2 | Municipality 4 | Barangay 8 |
JSON Format:
regions = [
{
name: 'Region 1',
provinces: [
{
name: 'Province 1',
municipalities: [
{
name: 'Municipality 1',
barangays: [
'Barangay 1',
'Barangay 2',
// Add more barangays here
]
},
{
name: 'Municipality 2',
barangays: [
'Barangay 3',
'Barangay 4',
// Add more barangays here
]
},
// Add more municipalities here
]
},
{
name: 'Province 2',
municipalities: [
{
name: 'Municipality 3',
barangays: [
'Barangay 5',
'Barangay 6',
// Add more barangays here
]
},
{
name: 'Municipality 4',
barangays: [
'Barangay 7',
'Barangay 8',
// Add more barangays here
]
},
// Add more municipalities here
]
},
// Add more provinces here
]
}
// Add more regions here
];
I tried df.to_json(orient="records")
. And I tried split
, records
, index
, columns
, values
, table
parameters but not the way I need. It will be used for dependent dropdown in form.