Как проанализировать указанные c части вложенного JSON формата в CSV в python (pandas) - PullRequest
0 голосов
/ 22 января 2020

У меня есть вложенный файл JSON, который мне не удается разобрать в flatten csv. Я хочу, чтобы в csv были следующие столбцы: id, имя, путь, теги (столбец для каждого из них), точки (мне нужны значения x \ y из 4 точек)

пример JSON input:

{
"name": "test",
"securityToken": "test Token",
"videoSettings": {
    "frameExtractionRate": 15
},
"tags": [
    {
        "name": "Blur Reject",
        "color": "#FF0000"
    },
    {
        "name": "Blur Poor",
        "color": "#800000"
    }
],
"id": "Du1qtrZQ1",
"activeLearningSettings": {
    "autoDetect": false,
    "predictTag": true,
    "modelPathType": "coco"
},
"version": "2.1.0",
"lastVisitedAssetId": "ddee3e694ec299432fed9e42de8741ad",
"assets": {
    "0b8f6f214dc7066b00b50ae16cf25cf6": {
        "asset": {
            "format": "jpg",
            "id": "0b8f6f214dc7066b00b50ae16cf25cf6",
            "name": "1.jpg",
            "path": "c:\temp\1.jpg",
            "size": {
                "width": 1500,
                "height": 1125
            },
            "state": 2,
            "type": 1
        },
        "regions": [
            {
                "id": "VtDyR9Ovl",
                "type": "POLYGON",
                "tags": [
                    "3",
                    "9",
                    "Dark Poor"
                ],
                "boundingBox": {
                    "height": 695.2110389610389,
                    "width": 1111.607142857143,
                    "left": 167.41071428571428,
                    "top": 241.07142857142856
                },
                "points": [
                    {
                        "x": 167.41071428571428,
                        "y": 252.02922077922076
                    },
                    {
                        "x": 208.80681818181816,
                        "y": 891.2337662337662
                    },
                    {
                        "x": 1252.232142857143,
                        "y": 936.2824675324675
                    },
                    {
                        "x": 1279.017857142857,
                        "y": 241.07142857142856
                    }
                ]
            }
        ],
        "version": "2.1.0"
    },
    "0155d8143c8cad85b5b9d392fd2895a4": {
        "asset": {
            "format": "jpg",
            "id": "0155d8143c8cad85b5b9d392fd2895a4",
            "name": "2.jpg",
            "path": "c:\temp\2.jpg",
            "size": {
                "width": 1080,
                "height": 1920
            },
            "state": 2,
            "type": 1
        },
        "regions": [
            {
                "id": "7FFl_diM2",
                "type": "POLYGON",
                "tags": [
                    "Dark Poor"
                ],
                "boundingBox": {
                    "height": 502.85714285714283,
                    "width": 820.3846153846155,
                    "left": 144.08653846153848,
                    "top": 299.2207792207792
                },
                "points": [
                    {
                        "x": 152.39423076923077,
                        "y": 311.68831168831167
                    },
                    {
                        "x": 144.08653846153848,
                        "y": 802.077922077922
                    },
                    {
                        "x": 964.4711538461539,
                        "y": 781.2987012987012
                    },
                    {
                        "x": 935.3942307692308,
                        "y": 299.2207792207792
                    }
                ]
            }
        ],
        "version": "2.1.0"
    }

}

Я попытался использовать json_normalize pandas и понял, что не до конца понимаю, как указать столбцы, которые я буду sh анализировать:

import json
import csv
import pandas as pd
from pandas import Series, DataFrame
from pandas.io.json import json_normalize 


f = open(r'c:\temp\test-export.json') 
data = json.load(f) # load as json
f.close()
df = json_normalize(data) #load json into dataframe
df.to_csv(r'c:\temp\json-to-csv.csv', sep=',', encoding='utf-8') 

С результатами трудно работать, потому что я не указал, что я хочу (итерируйте через массив c и добавьте его в CSV) Здесь я sh получу вашу помощь.

Полагаю, я не до конца понимаю, как работает нормализация, и подозреваю, что это не лучший способ справиться с этой проблемой.

Спасибо!

1 Ответ

0 голосов
/ 22 января 2020

Вы можете сделать что-то вроде этого. Поскольку вы не предоставили пример вывода, я сделал что-то самостоятельно.

import json
import csv

f = open(r'file.txt')
data = json.load(f)
f.close()
with open("output.csv", mode="w", newline='') as out:
    w = csv.writer(out)
    header = ["id","name","path","tags","points"]
    w.writerow(header)
    for asset in data["assets"]:
        data_point = data["assets"][asset]
        output = [data_point["asset"]["id"]]
        output.append(data_point["asset"]["name"])
        output.append(data_point["asset"]["path"])
        output.append(data_point["regions"][0]["tags"])
        output.append(data_point["regions"][0]["points"])
        w.writerow(output)

Вывод

id,name,path,tags,points
0b8f6f214dc7066b00b50ae16cf25cf6,1.jpg,c:\temp\1.jpg,"['3', '9', 'Dark Poor']","[{'x': 167.41071428571428, 'y': 252.02922077922076}, {'x': 208.80681818181816, 'y': 891.2337662337662}, {'x': 1252.232142857143, 'y': 936.2824675324675}, {'x': 1279.017857142857, 'y': 241.07142857142856}]"
0155d8143c8cad85b5b9d392fd2895a4,2.jpg,c:\temp\2.jpg,['Dark Poor'],"[{'x': 152.39423076923077, 'y': 311.68831168831167}, {'x': 144.08653846153848, 'y': 802.077922077922}, {'x': 964.4711538461539, 'y': 781.2987012987012}, {'x': 935.3942307692308, 'y': 299.2207792207792}]"

...