如何在python上改变json数据的结构和添加新字段

2024-09-28 18:12:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试修改json数据的结构,但我无法理解它。这是我掌握的数据样本

[<OldSample {u'counter_name': u'cpu_util', u'user_id': u'7bffa12f482840c7801e3e01e160c8cb', u'resource_id': u'ef392c3d-74fa-43fe-87c5-7e117b6d8a09', u'timestamp': u'2015-07-01T15:13:55', u'counter_volume': 0.034999999999999996, u'resource_metadata': {u'ramdisk_id': u'None', u'flavor.vcpus': u'1', u'OS-EXT-AZ.availability_zone': u'nova', u'display_name': u'ubuntu-io', u'flavor.id': u'596642d8-0813-4ae9-aec4-0105fdf05761', u'status': u'active', u'ephemeral_gb': u'0', u'flavor.name': u'm1.small.io', u'disk_gb': u'20', u'kernel_id': u'None', u'image.id': u'5776a360-0953-4c93-931d-a6e3616fb8dc', u'flavor.ram': u'2048', u'host': u'9fa544f5c47569db21d50bc6c0765296316a56bd6baf6b04d705686a', u'flavor.ephemeral': u'0', u'image.name': u'ubuntu-io', u'image_ref_url': u'link': u"[{'href': 'link', 'rel': 'bookmark'}]", u'cpu_number': u'1', u'flavor.disk': u'20', u'root_gb': u'20', u'name': u'instance-000000d3', u'memory_mb': u'2048', u'instance_type': u'596642d8-0813-4ae9-aec4-0105fdf05761', u'vcpus': u'1', u'image_ref': u'5776a360-0953-4c93-931d-a6e3616fb8dc', u'flavor.links': u"[{'href': 'link', 'rel': 'bookmark'}]"}, u'source': u'openstack', u'counter_unit': u'%', u'recorded_at': u'2015-07-01T15:13:56.006000', u'project_id': u'1670f0e56fb6421cb83d81b60b149c04', u'message_id': u'ca8ea466-2003-11e5-a764-002590e64886', u'counter_type': u'gauge'}>, <OldSample {u'counter_name': u'cpu_util', u'user_id': u'7bffa12f482840c7801e3e01e160c8cb', u'resource_id': u'0c8b6b26-3340-41e3-ac8b-cc38f15d3570', u'timestamp': u'2015-07-01T15:08:32', u'counter_volume': 5.4399999999999995, u'resource_metadata': {u'ramdisk_id': u'None', u'flavor.vcpus': u'1', u'OS-EXT-AZ.availability_zone': u'nova', u'display_name': u'kalman_instance', u'flavor.id': u'1', u'status': u'active', u'ephemeral_gb': u'0', u'flavor.name': u'm1.tiny', u'disk_gb': u'1', u'kernel_id': u'None', u'image.id': u'1c9b08f0-d1fa-4acc-a11c-87b77310158c', u'flavor.ram': u'512', u'host': u'25aa71ded460ea9d4bf52e1aac34017691699cb5e4e389704d738bed', u'flavor.ephemeral': u'0', u'image.name': u'cirros', u'image_ref_url': u'http://192.168.26.1:8774/d1d65b6feab741a6a2905e6197cb15ee/images/1c9b08f0-d1fa-4acc-a11c-87b77310158c', u'image.links': u"[{'href': 'http://192.168.26.1:8774/d1d65b6feab741a6a2905e6197cb15ee/images/1c9b08f0-d1fa-4acc-a11c-87b77310158c', 'rel': 'bookmark'}]", u'cpu_number': u'1', u'flavor.disk': u'1', u'root_gb': u'1', u'name': u'instance-0000013c', u'memory_mb': u'512', u'instance_type': u'1', u'vcpus': u'1', u'image_ref': u'1c9b08f0-d1fa-4acc-a11c-87b77310158c', u'flavor.links': u"[{'href': 'http://192.168.26.1:8774/d1d65b6feab741a6a2905e6197cb15ee/flavors/1', 'rel': 'bookmark'}]"}, u'source': u'openstack', u'counter_unit': u'%', u'recorded_at': u'2015-07-01T15:08:32.459000', u'project_id': u'1670f0e56fb6421cb83d81b60b149c04', u'message_id': u'09b5173e-2003-11e5-ac7a-002590e64b12', u'counter_type': u'gauge'}>]

我想把它改成这样

 message: {
      columns: [
        ["y": 0.043, "x": "2015-06-30T15:53:55"],
        ["y": 0.045, "x": "2015-06-30T15:53:55"]
      ]

这是我的密码

def clean_Json(data):
  for each in data:
    timestamp = each.timestamp
    volume =  each.counter_volume
    i = json.dumps({'x': timestamp, 'y': volume})
    print i

clean_Json(data)

结果:

{"y": 4.101666666666667, "x": "2015-04-10T15:18:18"}
{"y": 5.471666666666666, "x": "2015-04-10T14:48:18"}

问题是

1.每行末尾没有逗号。 2.当我尝试添加方括号时,会出现语法错误。你知道吗

我似乎无法从json.dumps文件({x':时间戳,'y':卷})


Tags: instancenameimagenoneidcountercpuresource
3条回答

我就是这样解决上述问题的:

thing = {}
msg = {}
cols = []


for row in data:
    col = {}
    col = {"x": row.timestamp, "y": row.counter_volume}
    cols.append(col)

msg['columns'] = cols

thing['message'] = msg

print json.dumps(thing, indent=4)

在JSON中,[]表示数组,{}表示对象。不可能有一个带有键值对的数组,就像在columns数组中的单个对象的所需输出中那样。它们是对象,因此必须仅包含在{}中。如果确实需要方括号,则必须实现自己的字符串连接逻辑。你知道吗

def get_clean_Json(data):
  return [{ "x": each['timestamp'], "y": each['counter_volume'] } for each in data]

测试上述代码:

st = '[{"timestamp": "2015-06-30T15:53:55", "counter_volume": 0.043}, {"timestamp": "121", "counter_volume": 0.045}]'
data = json.loads(st)
j = get_clean_Json(data)

json.dumps({ 'message': { 'columns': j } })

输出:

{"message": {"columns": [{"y": 0.043, "x": "2015-06-30T15:53:55"}, {"y": 0.045, "x": "121"}]}}

这个怎么样?你知道吗

result_rows = []
for row in data:
    # Format output as desired
    formatted = """["y": %0.3f, "x": "%s"]""" % (row.counter_volume, row.timestamp)

    #Append to result list
    result_rows.append(formatted)

# Pythonic way to join strings putting something (comma+newline) between items
print(',\n'.join(result_rows))

相关问题 更多 >