Python:展平和解析JSON

时间:2016-10-11 15:14:44

标签: python json parsing flatten

我有一个输入JSON,如下所示:

> {"payment": {"payment_id": "AA340", "payment_amt": "20", "chk_nr": "321749", "clm_list": {"dtl": [{"clm_id": "1A2345", "name": "John", adj:{"adj_id":"W123","adj_cd":"45"}}, {"clm_id": "9999", "name": "Dilton", adj:{"adj_id":"X123","adj_cd":"5"}}]}}}

我需要输出看起来像这样:

{"clm_id": "1A2345",adj:{"adj_id":"W123"},"payment_amt": "20", "chk_nr": "321749"}
{"clm_id": "9999"adj:{"adj_id":"X123"},"payment_amt": "20", "chk_nr": "321749"}

因此代码接受一个JSON文档,解析声明数组部分并通过向每个部分添加付款信息来规范化它。甚至嵌套的JSON也被解析。

我能够解析数据,但不确定如何仅规范化数据的某些部分。

下面的代码将解析数据,但不会标准化

keep = ["payment","payment_id","payment_amt", "clm_list", "dtl", "clm_id","adj","adj_id"]

old_dict={"payment": {"payment_id": "AA340", "payment_amt": "20", "chk_nr": "321749", "clm_list": {"dtl": [{"clm_id": "1A2345", "name": "John", "adj": {"adj_id": "W123", "adj_cd": "45"}}, {"clm_id": "9999", "name": "Dilton", "adj": {"adj_id": "X123", "adj_cd": "5"}}]}}}

def recursively_prune_dict_keys(obj, keep):
        if isinstance(obj, dict):
                return dict([(k, recursively_prune_dict_keys(v, keep)) for k, v in obj.items() if k in keep])
        elif isinstance(obj, list):
                return [recursively_prune_dict_keys(item, keep) for item in obj]
        else:
                return obj

new_dict = recursively_prune_dict_keys(old_dict, keep)
conv_json=new_dict["payment"]
print json.dumps(conv_json)

1 个答案:

答案 0 :(得分:0)

可能简单的方法是简单地选择数据,例如;

package com.infiniteskills.data.entities;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="FINANCES_USER")
public class User {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="USER_ID")
    private Long userId;

    @Column(name="FIRST_NAME")
    private String firstName;

    @Column(name="LAST_NAME")
    private String lastName;

    @Column(name="BIRTH_DATE")
    private Date birthDate;

    @Column(name="EMAIL_ADDRESS")
    private String emailAddress;

    @Column(name="LAST_UPDATE_DATE")
    private Date lastUpdateDate;

    @Column(name="LAST_UPDATED_BY")
    private String lastUpdatedBy;

    @Column(name="CREATED_DATE")
    private Date cteatedDate;

    @Column(name="CREATED_BY")
    private String createdBy;

    public Long getUserId() {
        return userId;
    }
    public void setUserId(Long userId) {
        this.userId = userId;
    }
    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public Date getBirthDate() {
        return birthDate;
    }
    public void setBirthDate(Date birthDate) {
        this.birthDate = birthDate;
    }
    public String getEmailAddress() {
        return emailAddress;
    }
    public void setEmailAddress(String emailAddress) {
        this.emailAddress = emailAddress;
    }
    public Date getLastUpdateDate() {
        return lastUpdateDate;
    }
    public void setLastUpdateDate(Date lastUpdateDate) {
        this.lastUpdateDate = lastUpdateDate;
    }
    public String getLastUpdatedBy() {
        return lastUpdatedBy;
    }
    public void setLastUpdatedBy(String lastUpdatedBy) {
        this.lastUpdatedBy = lastUpdatedBy;
    }
    public Date getCteatedDate() {
        return cteatedDate;
    }
    public void setCteatedDate(Date cteatedDate) {
        this.cteatedDate = cteatedDate;
    }
    public String getCreatedBy() {
        return createdBy;
    }
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }   
}

这将产生;

new_dict = recursively_prune_dict_keys(old_dict, keep)
payment = old_dict['payment']
claims = payment['clm_list']['dtl']
for claim in claims:
    claim['payment_amt'] = payment['payment_amt']
    claim['chk_nr'] = payment['chk_nr']
print(json.dumps(claims))

这包含您要求的输出,但不完全是您想要查看的输出。

首先,您所需的输出不是正确的JSON,而没有方括号[{"chk_nr": "321749", "clm_id": "1A2345", "payment_amt": "20", "adj": {"adj_id": "W123"}}, {"chk_nr": "321749", "clm_id": "9999", "payment_amt": "20", "adj": {"adj_id": "X123"}}] 会使其成为列表。但是,我们可以通过单独倾销每项索赔来摆脱这种情况;

[]

这给出了;

new_dict = recursively_prune_dict_keys(old_dict, keep)
payment = old_dict['payment']
claims = payment['clm_list']['dtl']
for claim in claims:
    claim['payment_amt'] = payment['payment_amt']
    claim['chk_nr'] = payment['chk_nr']
    print(json.dumps(claim))

这可能接近您想要的输出,除了订购。 Python dicts本身并不是有序的。但是,您可以对它们进行排序。因此,如果排序很重要,您需要阅读How to Sort Python Dictionaries by Key or Value