JSON转换为所需格式

时间:2019-08-21 09:09:28

标签: json mapping

我们正在一个中间件平台上工作,在该平台上,我们需要使用特定格式的JSON数据响应消费者。

我们从南向API获取的数据是一个键值对,需要将其映射为消费者可以理解的格式

我们尝试了json-path,ObjectMapper,但是它们都没有给我们带来预期的转换结果

从后端API响应

{
  "details": [
    {
      "name": "x.y.z.name","value": "TR-54695"
    },
    {
      "name": "a.b.c.standards","value": "DOCSIS"
    },
    {
      "name": "x.x.x.hversion","value": "10"
    },
    {
      "name": "x.x.x.sversion","value": "9.1.116V"
    },
    {
      "name": "x.x.x.uptime","value": "8000"
    },
    {
      "name": "x.x.x.accessallowed","value": "true"
    },

  ]
}

要转换为

{
  "myData": {
    "myInfo": {
      "productClass": "TR-54695",
      "supportedStandards": "DOCSIS",
      "hardwareVersion": "10",
      "softwareVersion": "9.1.116V",
      "modemMacAddress": "",
      "upTime": "8000",
      "modemNetworkAccessAllowed": true
    }
  }
}

2 个答案:

答案 0 :(得分:1)

您可以使用以下代码,并使用codesandbox link(检查控制台输出)获得准确的响应,并使用this link获得key:value对。

let response = {
  details: [
    {
      name: "x.y.z.name",
      value: "TR-54695"
    },
    {
      name: "a.b.c.standards",
      value: "DOCSIS"
    },
    {
      name: "x.x.x.hversion",
      value: "10"
    },
    {
      name: "x.x.x.sversion",
      value: "9.1.116V"
    },
    {
      name: "x.x.x.uptime",
      value: "8000"
    },
    {
      name: "x.x.x.accessallowed",
      value: "true"
    }
  ]
};

// convert function for key value pair
function convertResponse(responseData) {
  let output = { myData: { myInfo: {} } };
  let outputRef = output.myData.myInfo;
  responseData.forEach(element => {
    outputRef[element.name] = element.value
  });
  return output;
}

// OR convert Function for getting exact same output
function convertResponse(responseData) {
  let output = { myData: { myInfo: {} } };
  let outputRef = output.myData.myInfo;
  responseData.forEach(element => {
    if (element.name === "x.y.z.name") {
      outputRef.productClass = element.value;
    } else if (element.name === "a.b.c.standards") {
      outputRef.supportedStandards = element.value;
    } else if (element.name === "x.x.x.hversion") {
      outputRef.hardwareVersion = element.value;
    } else if (element.name === "x.x.x.sversion") {
      outputRef.softwareVersion = element.value;
    } else if (element.name === "x.x.x.uptime") {
      outputRef.upTime = element.value;
    } else if (element.name === "x.x.x.accessallowed") {
      outputRef.modemNetworkAccessAllowed = element.value;
    }
  });

  return output;
}

//Function Call
console.log(convertResponse(response.details));

答案 1 :(得分:1)

不喜欢手动工作,因此这里使用2个函数生成了演示。

请在投票下打勾接受按钮,以备不时之需。

function translate(src, mapping) {
    var dst = { "myData": { "myInfo": { "modemMacAddress": "" } } }
    //in case order matters:
    dst = { "myData": { "myInfo": { "productClass": "", "supportedStandards": "", "hardwareVersion": "", "softwareVersion": "", "modemMacAddress": "", "upTime": "", "modemNetworkAccessAllowed": undefined } } }
    var trueFalse = { "false": false, "true": true };
    src = src.details;
    for (var i = 0; i < src.length; i++) {
        dst.myData.myInfo[mapping[src[i].name]] = trueFalse[src[i].value] || src[i].value;
    }
    return dst;
}

function generateMapping(src, dst) {
    src = src.details;
    var backLinks = {}, rename2 = {};
    for (var i = 0; i < src.length; i++) {
        backLinks[src[i].value] = src[i].name;
    }
    dst = dst.myData.myInfo;
    for (var i in dst) {
        rename2[backLinks[dst[i]]] = i;
    }
    return rename2;
}

var src = {
    "details": [
        { "name": "x.y.z.name", "value": "TR-54695" },
        { "name": "a.b.c.standards", "value": "DOCSIS" },
        { "name": "x.x.x.hversion", "value": "10" },
        { "name": "x.x.x.sversion", "value": "9.1.116V" },
        { "name": "x.x.x.uptime", "value": "8000" },
        { "name": "x.x.x.accessallowed", "value": "true" },

    ]
}
var dst = {
    "myData": {
        "myInfo": {
            "productClass": "TR-54695",
            "supportedStandards": "DOCSIS",
            "hardwareVersion": "10",
            "softwareVersion": "9.1.116V",
            "modemMacAddress": "",
            "upTime": "8000",
            "modemNetworkAccessAllowed": true
        }
    }
}

var mapping = generateMapping(src, dst);
// var mapping = {
//     "x.y.z.name": "productClass",
//     "a.b.c.standards": "supportedStandards",
//     "x.x.x.hversion": "hardwareVersion",
//     "x.x.x.sversion": "softwareVersion",
//     "undefined": "modemMacAddress",
//     "x.x.x.uptime": "upTime",
//     "x.x.x.accessallowed": "modemNetworkAccessAllowed"
// }
var result = translate(src, mapping);
console.log(JSON.stringify(result, null, 2));
console.log(JSON.stringify(mapping, null, 2));