Python json清理并删除坏列和不必要的列表
我有以下Python json清理并删除坏列和不必要的列表,python,dictionary,Python,Dictionary,我有以下json: { "pfe-statistics" : [ { "pfe-traffic-statistics" : [ { "pfe-input-packets" : [ { "data" : "3895887" } ], "input-pps" : [ {
json
:
{
"pfe-statistics" : [
{
"pfe-traffic-statistics" : [
{
"pfe-input-packets" : [
{
"data" : "3895887"
}
],
"input-pps" : [
{
"data" : "0"
}
],
"pfe-output-packets" : [
{
"data" : "1114541"
}
],
"output-pps" : [
{
"data" : "0"
}
]
}
],
"pfe-local-traffic-statistics" : [
{
"pfe-input-packets" : [
{
"data" : "3893502"
}
],
"pfe-output-packets" : [
{
"data" : "1114541"
}
],
"software-input-control-drops" : [
{
"data" : "0"
}
],
"software-input-high-drops" : [
{
"data" : "0"
}
],
"software-input-medium-drops" : [
{
"data" : "0"
}
],
"software-input-low-drops" : [
{
"data" : "0"
}
],
"software-output-low-drops" : [
{
"data" : "0"
}
],
"hardware-input-drops" : [
{
"data" : "0"
}
]
}
],
"pfe-local-protocol-statistics" : [
{
"hdlc-keepalive-count" : [
{
"data" : "0"
}
],
"atm-oam-count" : [
{
"data" : "0"
}
],
"fr-lmi-count" : [
{
"data" : "0"
}
],
"ppp-lcp-ncp-count" : [
{
"data" : "0"
}
],
"ospf-hello-count" : [
{
"data" : "0"
}
],
"ospf3-hello-count" : [
{
"data" : "0"
}
],
"rsvp-hello-count" : [
{
"data" : "0"
}
],
"ldp-hello-count" : [
{
"data" : "0"
}
],
"bfd-count" : [
{
"data" : "0"
}
],
"isis-iih-count" : [
{
"data" : "0"
}
],
"lacp-count" : [
{
"data" : "0"
}
],
"arp-count" : [
{
"data" : "0"
}
],
"ether-oam-count" : [
{
"data" : "0"
}
],
"unknown-count" : [
{
"data" : "0"
}
]
}
],
"pfe-hardware-discard-statistics" : [
{
"timeout-discard" : [
{
"data" : "0"
}
],
"truncated-key-discard" : [
{
"data" : "0"
}
],
"bits-to-test-discard" : [
{
"data" : "0"
}
],
"data-error-discard" : [
{
"data" : "0"
}
],
"stack-underflow-discard" : [
{
"data" : "0"
}
],
"stack-overflow-discard" : [
{
"data" : "0"
}
],
"bad-route-discard" : [
{
"data" : "0"
}
],
"nexthop-discard" : [
{
"data" : "0"
}
],
"invalid-iif-discard" : [
{
"data" : "0"
}
],
"info-cell-discard" : [
{
"data" : "0"
}
],
"fabric-discard" : [
{
"data" : "0"
}
]
}
],
"pfe-chip-statistics" : [
{
"input-checksum" : [
{
"data" : "0"
}
],
"output-mtu" : [
{
"data" : "0"
}
]
}
]
}
]
}
我想生成以下内容:
{
"pfe-statistics" :
{
"pfe-traffic-statistics" :
{
"pfe-input-packets" : "56601136",
"input-pps" : "30",
"pfe-output-packets" : "71427636",
"output-pps" : "28",
"pfe-fabric-input" : "0",
"pfe-fabric-input-pps" : "0",
"pfe-fabric-output" : "0",
"pfe-fabric-output-pps" : "0"
},
"pfe-local-traffic-statistics" :
{
"pfe-input-packets" : "56226309",
"pfe-output-packets" : "142458974",
"software-input-control-drops" : "0",
"software-input-high-drops" : "0",
"software-input-medium-drops" : "0",
"software-input-low-drops" : "0",
"software-output-low-drops" : "0",
"hardware-input-drops" : "0"
},
"pfe-local-protocol-statistics" :
{
"hdlc-keepalive-count" : "0",
"atm-oam-count" : "0",
"fr-lmi-count" : "0",
"ppp-lcp-ncp-count" : "0",
"ospf-hello-count" : "0",
"ospf3-hello-count" : "0",
"rsvp-hello-count" : "0",
"ldp-hello-count" : "0",
"bfd-count" : "49316136",
"isis-iih-count" : "0",
"lacp-count" : "0",
"arp-count" : "433208",
"ether-oam-count" : "0",
"unknown-count" : "0"
},
"pfe-hardware-discard-statistics" :
{
"timeout-discard" : "0",
"truncated-key-discard" : "0",
"bits-to-test-discard" : "0",
"data-error-discard" : "0",
"tcp-header-error-discard" : "0",
"stack-underflow-discard" : "0",
"stack-overflow-discard" : "0",
"bad-route-discard" : "0",
"nexthop-discard" : "0",
"invalid-iif-discard" : "0",
"info-cell-discard" : "0",
"fabric-discard" : "0"
},
"pfe-chip-statistics" :
{
"input-checksum" : "0",
"output-mtu" : "0"
}
}
}
我编写了以下函数,但无法使其100%工作:
d = {}
l = []
def compact_json(df):
for k, v in df.items():
if isinstance(v, list):
print("list")
print(k)
if "data" not in v[0].keys():
val = v[0].keys()
if val not in l:
l.append(k)
compact_json(v[0])
#d.update(k)
if isinstance(v[0], dict):
print("dict")
if "data" in v[0].keys():
print("if data yes")
print(k, v[0]["data"])
d1 = {k: v}
#d[val] = v[0]["data"]
d[k] = v[0]["data"]
else:
print("else no data")
print(v[0])
compact_json(v[0])
compact_json(df)
print("==============")
print(d)
我只能打印树叶,不能打印树枝。请注意,我想使其通用于多个
json
,因此所有形式的嵌套深度都可能不同。您可以使用嵌套字典:
result = {a:{j:{l:b['data'] for y in k for l, [b] in y.items()}
for g in c for j, k in g.items()} for a, c in data.items()}
输出:
{
"pfe-statistics": {
"pfe-traffic-statistics": {
"pfe-input-packets": "3895887",
"input-pps": "0",
"pfe-output-packets": "1114541",
"output-pps": "0"
},
"pfe-local-traffic-statistics": {
"pfe-input-packets": "3893502",
"pfe-output-packets": "1114541",
"software-input-control-drops": "0",
"software-input-high-drops": "0",
"software-input-medium-drops": "0",
"software-input-low-drops": "0",
"software-output-low-drops": "0",
"hardware-input-drops": "0"
},
"pfe-local-protocol-statistics": {
"hdlc-keepalive-count": "0",
"atm-oam-count": "0",
"fr-lmi-count": "0",
"ppp-lcp-ncp-count": "0",
"ospf-hello-count": "0",
"ospf3-hello-count": "0",
"rsvp-hello-count": "0",
"ldp-hello-count": "0",
"bfd-count": "0",
"isis-iih-count": "0",
"lacp-count": "0",
"arp-count": "0",
"ether-oam-count": "0",
"unknown-count": "0"
},
"pfe-hardware-discard-statistics": {
"timeout-discard": "0",
"truncated-key-discard": "0",
"bits-to-test-discard": "0",
"data-error-discard": "0",
"stack-underflow-discard": "0",
"stack-overflow-discard": "0",
"bad-route-discard": "0",
"nexthop-discard": "0",
"invalid-iif-discard": "0",
"info-cell-discard": "0",
"fabric-discard": "0"
},
"pfe-chip-statistics": {
"input-checksum": "0",
"output-mtu": "0"
}
}
}
不相关,但一旦您取消序列化json字符串,它就不再是json了,只是一个普通的python对象-因此它来自json字符串的事实是完全不相关的,在同一个json中,我必须将其应用于多个具有不同深度和不同层次上的不同深度的json,这是什么?
{
"pfe-statistics": {
"pfe-traffic-statistics": {
"pfe-input-packets": "3895887",
"input-pps": "0",
"pfe-output-packets": "1114541",
"output-pps": "0"
},
"pfe-local-traffic-statistics": {
"pfe-input-packets": "3893502",
"pfe-output-packets": "1114541",
"software-input-control-drops": "0",
"software-input-high-drops": "0",
"software-input-medium-drops": "0",
"software-input-low-drops": "0",
"software-output-low-drops": "0",
"hardware-input-drops": "0"
},
"pfe-local-protocol-statistics": {
"hdlc-keepalive-count": "0",
"atm-oam-count": "0",
"fr-lmi-count": "0",
"ppp-lcp-ncp-count": "0",
"ospf-hello-count": "0",
"ospf3-hello-count": "0",
"rsvp-hello-count": "0",
"ldp-hello-count": "0",
"bfd-count": "0",
"isis-iih-count": "0",
"lacp-count": "0",
"arp-count": "0",
"ether-oam-count": "0",
"unknown-count": "0"
},
"pfe-hardware-discard-statistics": {
"timeout-discard": "0",
"truncated-key-discard": "0",
"bits-to-test-discard": "0",
"data-error-discard": "0",
"stack-underflow-discard": "0",
"stack-overflow-discard": "0",
"bad-route-discard": "0",
"nexthop-discard": "0",
"invalid-iif-discard": "0",
"info-cell-discard": "0",
"fabric-discard": "0"
},
"pfe-chip-statistics": {
"input-checksum": "0",
"output-mtu": "0"
}
}
}