简体   繁体   中英

I am searching to convert complex json to csv using python or R

It would be helpful if values are converted as rows of CSV and keys are received a columns of CSV.

{
    "_id": {
        "$uId”: “12345678”
    },
    “comopany_productId”: “J00354”,
    “`company_product name`”: “BIKE 12345”,
    "search_results": [
        {
            “product_id”: "44zIVQ",
            "constituents”: [
                {
                    “tyre”: “2”,
                    "name": “dunlop”
                },
                {
                    "strength": “able to move 100 km”,
                    "name": “MRF”
                }
            ],
            "name": “Yhakohuka”,
            "form": “tyre”,
            "schedule": {
                "category": “a”,
                "label": "It needs a good car to fit in”
            },
            "standardUnits": 20,
            "price": 2000,
            "search_score”:0.947474,
            “Form”: “tyre”,
            "manufacturer": “hum”,
            "id": “12345678”,
            "size": “4”
        },

I want uId, company_productId”, "company_product name", various keys in search_results “tyre”,"name",""strength","name","form","schedule","category","label","standard units","price","search_score","Form","manufacturer","id","size" ias difference column in excel and values as rows.

In python you can use libraries pandas and json to convert it to a csv like this:

from pandas.io.json import json_normalize
import json

json_normalize(json.loads('your_json_string')).to_csv('file_name.csv')

If you have your json saved on a file, use json.load instead, passing the file object to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM