How to remove part of a JSON file

3

I need to remove parts of a JSON, but I do not know how I can do it.

I'm converting the same to CSV, and when I convert the structure it gets all weird

Here is the print explaining better, which is marked in black, I need to remove it and the one in red I need to keep.

Here'sjsonforwhatIneedtoconvert:

{"@odata.context":"url requisição",
    "value":[
        {
            "cprq_cert_id":51,
            "cprq_object_id":"xxxxxxxxxxxxxxxxxxxx",
            "cprq_cert_cert_id":0
        },
        {
            "cprq_cert_id":52,
            "cprq_object_id":"xxxxxxxxxxxxxxxxxxxx",
            "cprq_cert_cert_id":0
        },
        {
            "cprq_cert_id":54,
            "cprq_object_id":"xxxxxxxxxxxxxxxxxxxx",
            "cprq_cert_cert_id":0
        },
        {
            "cprq_cert_id":72,
            "cprq_object_id":"xxxxxxxxxxxxxxxxxxxx",
            "cprq_cert_cert_id":0
        },
        {
            "cprq_cert_id":73,
            "cprq_object_id":"xxxxxxxxxxxxxxxxxxxx",
            "cprq_cert_cert_id":0
        }
    ]
}

And also the python script I use to convert:

import csv
import json
infile = open("nomejson.json","r")
outfile = open("nomecsv.csv","w")

writer = csv.writer(outfile)

for row in json.loads(infile.read()):
    writer.writerow(row)
    
asked by anonymous 05.10.2017 / 19:20

1 answer

2

json2csv.py:

import csv
import json

# Abre arquivo de entrada para leitura
with open("entrada.json", "r") as infile:

    # Carrega arquivo JSON de entrada
    data = json.load( infile )

# Abre arquivo de saida para gravacao
with open("saida.csv", "wb") as outfile:

    # Cria gravador de CSV...
    wr = csv.writer( outfile, delimiter=',' )

    # Para cada elemento da array "value"
    for item in data["value"]:

        # Escreve cabecalho somente na primeira iteracao
        if( data["value"].index(item) == 0 ):
            wr.writerow(item.keys())

        # Escreve linha com os dados
        wr.writerow(item.values())

entrada.json :

{
    "@odata.context":"https://jnj-dev-pilot.csod.com/services/api/x/odata/api/views/#vw_rpt_cert_prerequisite",
    "value":[
        {
            "cprq_cert_id":51,
            "cprq_object_id":"d0b7fae2-cc11-44d1-a45b-a39642e65c08",
            "cprq_cert_cert_id":0
        },
        {
            "cprq_cert_id":52,
            "cprq_object_id":"20ca15ff-7c6f-4938-a00a-7f17a2753a7b",
            "cprq_cert_cert_id":0
        },
        {
            "cprq_cert_id":54,
            "cprq_object_id":"d0b7fae2-cc11-44d1-a45b-a39642e65c08",
            "cprq_cert_cert_id":0
        },
        {
            "cprq_cert_id":72,
            "cprq_object_id":"d0b7fae2-cc11-44d1-a45b-a39642e65c08",
            "cprq_cert_cert_id":0
        },
        {
            "cprq_cert_id":73,
            "cprq_object_id":"d0b7fae2-cc11-44d1-a45b-a39642e65c08",
            "cprq_cert_cert_id":0
        }
    ]
}

saida.csv :

cprq_cert_id,cprq_cert_cert_id,cprq_object_id
51,0,d0b7fae2-cc11-44d1-a45b-a39642e65c08
52,0,20ca15ff-7c6f-4938-a00a-7f17a2753a7b
54,0,d0b7fae2-cc11-44d1-a45b-a39642e65c08
72,0,d0b7fae2-cc11-44d1-a45b-a39642e65c08
73,0,d0b7fae2-cc11-44d1-a45b-a39642e65c08
    
05.10.2017 / 20:55