一个文件中的Json值,基于另一个Json文件中的字段 [英] Json values from one file, based on fields from another Json file

查看:66
本文介绍了一个文件中的Json值,基于另一个Json文件中的字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据另一个JSON文件的字段映射将值获取到JSON文件.我有一个从外部系统获得的JSON文件,这些系统具有不同的字段名称.我创建了一个JSON文件,该文件会将外部系统中的字段映射到我需要字段位于的名称:

I am trying to get values to a JSON file, based on field mapping from another JSON file. I have a JSON file I get from external systems, which have different field names. I created a JSON file that will map the fields from the external system to the names I need the fields to be in:

{
    "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"
}

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

In this file, the fields are the fields I will need to populate, while the values are the names of the fields I get from the external system, which looks like this for example:

{
    "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文件将需要如下所示:

So the end JSON file will need to look like this:

{
    "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"
}

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

I was thinking of using a dictionary/index to create this, but I've never done that before, nor am I sure how to do it. Is there a simple way to do it?

推荐答案

当我们具有单个JSON级别时,使用字典很容易进行这种映射.

This kind of mapping is easy with a dictionary when we have single level of JSON.

对于多层JSON,我们可以使用递归函数来映射字典中的数据.

For a multi level JSON, we can make use of a recursive function to map the data from dictionary.

在代码中可以说这是您的JSON:

In the code lets say this is your 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);


结果:


Result:

{
    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文件中的字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆