解析嵌套的JSON键并以CSV格式获取值

时间:2018-07-05 20:13:56

标签: python json

我有一个嵌套的JSON数据,大约有5000条记录。

{
    "data": {
        "attributes": [
            {
                "alert_type": "download",
                "severity_level": "med",
                "user": "10.1.1.16"
            },
            {
                "alert_type": "download",
                "severity_level": "low",
                "user": "10.2.1.18"
            }
        ]
    }
}

现在,我需要解析此JSON并仅获取CSV格式的某些字段。让我们需要CSV格式的alert_typeuser

我试图解析此JSON字典:

>>> import json
>>> resp = '{"data":{"attributes":[{"alert_type":"download","severity_level":"med","user":"10.1.1.16"},{"alert_type":"download","severity_level":"low","user":"10.2.1.18"}]}}'
>>> user_dict = json.loads(resp)
>>> event_cnt = user_dict['data']['attributes']
>>> print event_cnt[0]['alert_type']
download
>>> print event_cnt[0]['user']
10.1.1.16
>>> print event_cnt[0]['alert_type'] + "," + event_cnt[0]['user']
download,10.1.1.16
>>>

如何以CSV格式并在一次迭代中获取特定values的所有元素/ keys

输出:

download,10.1.1.16
download,10.2.1.18

4 个答案:

答案 0 :(得分:1)

由于{"data":{"attributes":是一个列表,因此您可以在其上循环并打印所需键的值(d是用户命令):

for item in d['data']['attributes']:
    print(item['alert_type'],',',item['user'], sep='')

答案 1 :(得分:1)

您可以使它像这样由数据驱动:

"version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
  "dependencies": {
    "vue": "^2.5.16"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "^3.0.0-beta.15",
    "@vue/cli-plugin-eslint": "^3.0.0-beta.15",
    "@vue/cli-service": "^3.0.0-beta.15",
    "vue-template-compiler": "^2.5.16"
  },

要处理没有所有键的属性,您可以将其用作最后一行,该行将为缺失值分配默认值(例如所示的空白字符串),而不是引起异常。

import json

DESIRED_KEYS = 'alert_type', 'user'

resp = '''{ "data": {
                "attributes": [
                    {
                        "alert_type": "download",
                        "severity_level": "med",
                        "user": "10.1.1.16"
                    },
                    {
                        "alert_type": "download",
                        "severity_level": "low",
                        "user": "10.2.1.18"
                    }
                ]
            }
          }
       '''

user_dict = json.loads(resp)

for attribute in user_dict['data']['attributes']:
    print(','.join(attribute[key] for key in DESIRED_KEYS))

答案 2 :(得分:1)

简单的列表理解:

>>> jdict=json.loads(resp)
>>> ["{},{}".format(d["alert_type"],d["user"]) for d in jdict["data"]["attributes"]]
['download,10.1.1.16', 'download,10.2.1.18']

您可以加入其中以获得所需的输出:

>>> li=["{},{}".format(d["alert_type"],d["user"]) for d in jdict["data"]["attributes"]]
>>> print '\n'.join(li)
download,10.1.1.16
download,10.2.1.18

答案 3 :(得分:1)

使用,单行解决方案非常简单:

$ jq -r '.data.attributes[] | [.alert_type, .user] | @csv' input.json
"download","10.1.1.16"
"download","10.2.1.18"

如果您不想用引号引起来,请使用join(",")代替@csv