How to convert python data frame to json of fixed keys and dataframe columns as values

Required to convert dataframe values into json format which will be feed for an external reporting system. Sample dataframe values and required json format is shared below. Note: These Category can span to 1000 records or even more. Required to process the data efficiently

Data Frame Values

    **Category**   **catgName**    **catgCode**
    Vegies     Carrot  V001
    Fruit      Mango   F001
    Leaf       Mint    L001
    ExtFruit   Berry   EF01
    Fruit      Papaya  F002
    Vegies     Tomato  V002

Required Json Output

    {
        "externalFeed": {
            "Vegies": {
                "count": "2",
                "details": [{
                        "catgName": "Carrot",
                        "catgCode": "V001"
                    },
                    {
                        "catgName": "Tomato",
                        "catgCode": "V002"
                    }
                ]
            },
            "Fruit": {
                "count": "2",
                "details": [{
                        "catgName": "Mango",
                        "catgCode": "F001"
                    },
                    {
                        "catgName": "Papaya",
                        "catgCode": "F002"
                    }
                ]
            },
            "Leaf": {
                "count": "1",
                "details": [{
                            "catgName": "Mint",
                            "catgCode": "L001"
                        }
                    ]
            },
            "ExtFruit": {
                    "count": "1",
                    "details": [{
                            "catgName": "Berry",
                            "catgCode": "EF01"
                        }
                    ]
            }
        }
    }

Thanks in advance



Read more here: https://stackoverflow.com/questions/67014957/how-to-convert-python-data-frame-to-json-of-fixed-keys-and-dataframe-columns-as

Content Attribution

This content was originally published by lakksv at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: