【问题标题】:Json values from one file, based on fields from another Json file一个文件中的 Json 值,基于另一个 Json 文件中的字段
【发布时间】:2020-11-24 10:14:24
【问题描述】:

我正在尝试根据来自另一个 JSON 文件的字段映射来获取 JSON 文件的值。 我有一个从外部系统获得的 JSON 文件,它具有不同的字段名称。我创建了一个 JSON 文件,它将外部系统中的字段映射到我需要字段所在的名称:

{
    "username":"user",
    "externalSystemId":"id",
    "active":"active",
    "type":"type",
    "patronGroup":"group",
    "meta": {
        "creation_date":"dateCreated",
        "last_login_date":"lastLogin"
    },
    "personal": {
        "lastName":"lname",
        "firstName":"fname",
        "email":"email",
        "phone":"phone",
        "mobilePhone":"mobile",
        "dateOfBirth":"birthDate",
        "addresses":{
                "countryId": "countryCode",
                "addressLine1": "address",
                "addressLine2": "address2",
                "city": "city",
                "region": "region",
                "postalCode": "zipcode",
                "addressTypeId": "addressType",
                "primaryAddress": "primary"
        }
    },
    "enrollmentDate": "enrollmentDate",
    "expirationDate": "expirationDate"
}

在这个文件中,字段是我需要填充的字段,而值是我从外部系统获取的字段的名称,例如:

{
    "user":"name",
    "id":12345,
    "active":true,
    "type":"Student",
    "group":"BA",
    "dateCreated":"NOV 22 2019",
    "lastLogin":"NOV 23 2020",
    "lname":"Yard",
    "fname":"Paul",
    "email":"email@gmail.com",
    "phone":"(000)-000-0000",
    "mobile":"(000)-000-0000",
    "birthDate":"OCT 11 1999",
    "countryId": "US",
    "addressLine1": "4th street",
    "addressLine2": "25B",
    "city": "NY",
    "region": "NY",
    "postalCode": "00000",
    "addressTypeId": "Personal",
    "primaryAddress": true,
    "enrollmentDate": "MAR 22 2019",
    "expirationDate": "MAR 21 2022"
}

所以最终的 JSON 文件需要如下所示:

{
    "username":"name",
    "externalSystemId":12345,
    "active":true,
    "type":"Student",
    "patronGroup":"BA",
    "meta": {
        "creation_date":"NOV 22 2019",
        "last_login_date":"NOV 23 2020"
    },
    "personal": {
        "lastName":"Yard",
        "firstName":"Paul",
        "email":"email@gmail.com",
        "phone":"(000)-000-0000",
        "mobilePhone":"(000)-000-0000",
        "dateOfBirth":"OCT 11 1999",
        "addresses":{
            "countryId": "US",
            "addressLine1": "4th street",
            "addressLine2": "25B",
            "city": "NY",
            "region": "NY",
            "postalCode": "00000",
            "addressTypeId": "Personal",
            "primaryAddress": true
        }
    },
    "enrollmentDate": "MAR 22 2019",
    "expirationDate": "MAR 21 2022"
}

我正在考虑使用字典/索引来创建它,但我以前从未这样做过,也不知道该怎么做。 有简单的方法吗?

【问题讨论】:

    标签: node.js json dictionary indexing


    【解决方案1】:

    当我们有单级 JSON 时,这种映射很容易使用字典。

    对于多级 JSON,我们可以利用递归函数来映射字典中的数据。

    在代码中让我们说这是您的 JSON:

    let json = {
        username: "user",
        externalSystemId: "id",
        active: "active",
        type: "type",
        patronGroup: "group",
        meta: {
            creation_date: "dateCreated",
            last_login_date: "lastLogin",
        },
        personal: {
            lastName: "lname",
            firstName: "fname",
            email: "email",
            phone: "phone",
            mobilePhone: "mobile",
            dateOfBirth: "birthDate",
            addresses: {
                countryId: "countryCode",
                addressLine1: "address",
                addressLine2: "address2",
                city: "city",
                region: "region",
                postalCode: "zipcode",
                addressTypeId: "addressType",
                primaryAddress: "primary",
            },
        },
        enrollmentDate: "enrollmentDate",
        expirationDate: "expirationDate",
    };
    
    
    // Here is the dictionary:
    // Note: the personal address field had incorrect mapping for few fields, I have modified the property to match the value of respective key of the JSON.
    
    let dictionary = {
        user: "name",
        id: 12345,
        active: true,
        type: "Student",
        group: "BA",
        dateCreated: "NOV 22 2019",
        lastLogin: "NOV 23 2020",
        lname: "Yard",
        fname: "Paul",
        email: "email@gmail.com",
        phone: "(000)-000-0000",
        mobile: "(000)-000-0000",
        birthDate: "OCT 11 1999",
        countryCode: "US",
        address: "4th street",
        address2: "25B",
        city: "NY",
        region: "NY",
        zipcode: "00000",
        addressType: "Personal",
        primary: true,
        enrollmentDate: "MAR 22 2019",
        expirationDate: "MAR 21 2022",
    };
    
    
    
    // recursive function to map the JSON, extract the actual value from from dictionary
    function buildMapping(jsonParam) {
        let result = {};
        // iterate over the JSON parameter
        for (const [key, value] of Object.entries(jsonParam)) {
            if (typeof value === "object") {
                // for type as object, make a recursive call with the object value as param
                // collect the result of inner object
                let resultInner = buildMapping(value);
                // attach the result of the inner object to its key
                result[key] = resultInner;
            } else {
                // check if the dictionary contains the mapping, otherwise set as null
                result[key] = dictionary.hasOwnProperty(value)
                    ? dictionary[value]
                    : null;
            }
        }
        return result;
    }
    
    let resultJson = buildMapping(json);
    console.log(resultJson);
    

    结果:

    {
        username: "name",
        externalSystemId: 12345,
        active: true,
        type: "Student",
        patronGroup: "BA",
        meta: { creation_date: "NOV 22 2019", last_login_date: "NOV 23 2020" },
        personal: {
            lastName: "Yard",
            firstName: "Paul",
            email: "email@gmail.com",
            phone: "(000)-000-0000",
            mobilePhone: "(000)-000-0000",
            dateOfBirth: "OCT 11 1999",
            addresses: {
                countryId: "US",
                addressLine1: "4th street",
                addressLine2: "25B",
                city: "NY",
                region: "NY",
                postalCode: "00000",
                addressTypeId: "Personal",
                primaryAddress: true,
            },
        },
        enrollmentDate: "MAR 22 2019",
        expirationDate: "MAR 21 2022",
    };
    
    

    【讨论】:

    • 啊,我没有将所有内容都写在一个部分中,而是依次声明了输入 JSON、字典 JSON 和函数,这令人困惑。让我稍微改变一下格式。如果您检索字典并在您的 JS 文件中输入,那么您可以传递 JSON 参数并且可以访问字典。
    • jsonParamdictionary 是否在函数中正确打印?否则分享您最新代码的要点供我查看?
    • 我明白了,所以你给函数buildMapping添加了一个参数,你也应该在从内部调用函数时传递参数:let resultInner = buildMapping(value, dictionary);,现在你没有传递它,它得到一个未定义的值。
    • 另外你可能需要在server.js中切换字典和数据let test = buildMapping(settings.fieldMap, user);
    • 啊,有道理!现在我收到回复了!映射似乎有问题,因为有些字段返回为空,但其他字段已满,所以这可能不是代码问题而是映射问题,我可以弄清楚!非常感谢!
    猜你喜欢
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多