将扁平json转换为嵌套

时间:2017-04-20 14:24:24

标签: javascript json lodash

我有以下JSON

{
  "rate_cards": [
    {
      "doctor_id": "xxxxxx",
      "doctor_name": "Dr. Dre",
      "practice_id": "xxxxx",
      "is_owner": 0,
      "policy_id": 1,
      "policy_name": "GOLD Super Gold",
      "payor_id": 1,
      "payor_name": "AMEX",
      "payor_registration_number": "AMEX123",
      "payor_contact_email": "support@amex.com",
      "payor_support_number": "PHONE",
      "payor_logo_url": "url",
      "doctors_policies_ratecards_id": 1,
      "ratecard_activation_status": "Pending",
      "ratecard_id": 1,
      "ratecard_validity": "2017-04-17T13:16:58.000Z",
      "ratecard_details": "{'Root Canal': 300, 'Consultation': 500}",
      "onboarded_status": "Available"
    },
    {
      "doctor_id": xxxxxx,
      "doctor_name": "<NAME>",
      "practice_id": "xxxxxx",
      "is_owner": 0,
      "policy_id": 2,
      "policy_name": "GOLD Super Gold",
      "payor_id": 1,
      "payor_name": "AMEX",
      "payor_registration_number": "AMEX",
      "payor_contact_email": "support@amex.com",
      "payor_support_number": "<PHONE>",
      "payor_logo_url": "url",
      "doctors_policies_ratecards_id": 2,
      "ratecard_activation_status": "Pending",
      "ratecard_id": 1,
      "ratecard_validity": "2017-04-17T13:16:58.000Z",
      "ratecard_details": "{'Root Canal': 300, 'Consultation': 500}",
      "onboarded_status": "Available"
    }
  ]
}

我希望将其转换为

{
  "payors": [
      {
          "payor_id": 1,
          "payor_name": "AMEX",
          "payor_registration_number": "AMEX",
          "payor_contact_email": "support@amex.com",
          "payor_support_number": "<PHONE>",
          "payor_logo_url": "url",
          "data": [
              {
                  "doctor_id":xxxxx,
                  "doctor_name": "Dr. Dre",
                  "practice_id": xxxxxx,
                  "is_owner": 0,
                  "policy_id": 1,
                  "policy_name": "GOLD Super Gold",
                  "doctors_policies_ratecards_id": 1,
                  "ratecard_activation_status": "Pending",
                  "ratecard_id": 1,
                  "ratecard_validity": "2017-04-17T13:16:58.000Z",
                  "ratecard_details": "{'Root Canal': 300, 'Consultation': 500}",
                  "onboarded_status": "Available"
              }, 
              {
                  "doctor_id": xxxxxx,
                  "doctor_name": "name",
                  "practice_id": xxxxxxx,
                  "is_owner": 0,
                  "policy_id": 2,
                  "policy_name": "GOLD Super Gold",
                  "doctors_policies_ratecards_id": 2,
                  "ratecard_activation_status": "Pending",
                  "ratecard_id": 1,
                  "ratecard_validity": "2017-04-17T13:16:58.000Z",
                  "ratecard_details": "{'Root Canal': 300, 'Consultation': 500}",
                  "onboarded_status": "Available"
              }
          ]
      }
  ]
}

基本上我基于payor_name进行分组,这将成为我嵌套结构的主要参考。我尝试使用lodash groupBy和transform但无法实现上述结构。对我所能做的任何帮助都会很有帮助。

3 个答案:

答案 0 :(得分:1)

你可以通过lodash(跳过一些键,处理它们)来做到这一点 -

&#13;
&#13;
var data = {
  "rate_cards": [
    {
      "doctor_id": "xxxxxx",
      "doctor_name": "Dr. Dre",
      "practice_id": "xxxxx",
      "is_owner": 0,
      "policy_id": 1,
      "policy_name": "GOLD Super Gold",
      "payor_id": 1,
      "payor_name": "Amex",
      "payor_registration_number": "AMEX123",
      "payor_contact_email": "support@amex.com",
      "payor_support_number": "PHONE",
      "payor_logo_url": "url",
      "doctors_policies_ratecards_id": 1,
      "ratecard_activation_status": "Pending",
      "ratecard_id": 1,
      "ratecard_validity": "2017-04-17T13:16:58.000Z",
      "ratecard_details": "{'Root Canal': 300, 'Consultation': 500}",
      "onboarded_status": "Available"
    },
    {
      "doctor_id": "xxxxx",
      "doctor_name": "<NAME>",
      "practice_id": "xxxxxx",
      "is_owner": 0,
      "policy_id": 2,
      "policy_name": "GOLD Super Gold",
      "payor_id": 1,
      "payor_name": "AMEX",
      "payor_registration_number": "AMEX",
      "payor_contact_email": "support@amex.com",
      "payor_support_number": "<PHONE>",
      "payor_logo_url": "url",
      "doctors_policies_ratecards_id": 2,
      "ratecard_activation_status": "Pending",
      "ratecard_id": 1,
      "ratecard_validity": "2017-04-17T13:16:58.000Z",
      "ratecard_details": "{'Root Canal': 300, 'Consultation': 500}",
      "onboarded_status": "Available"
    }
  ]
};

var newdata = _.reduce(data.rate_cards,
    function( arr, item){
      var index = _.findIndex(arr, { "payor_id" : item.payor_id});
      if(index > -1){
        arr[index].data.push(item);
      } else {
        arr.push({ "payor_id" : item.payor_id, "data" : [item] });
      }
     return arr;
    },
    []
    );
    
console.log({ "payors" : newdata});
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

我遍历rate_cards,将付款人添加到我用id编制的字典中。对于每个付款人,我将对象的其余部分添加到数据数组中。

在循环之后,我将字典转换为数组并打印结果。

const input = {
  "rate_cards": [{
      "doctor_id": "xxxxxx",
      "doctor_name": "Dr. Dre",
      "practice_id": "xxxxx",
      "is_owner": 0,
      "policy_id": 1,
      "policy_name": "GOLD Super Gold",
      "payor_id": 1,
      "payor_name": "Amex",
      "payor_registration_number": "AMEX123",
      "payor_contact_email": "support@amex.com",
      "payor_support_number": "PHONE",
      "payor_logo_url": "url",
      "doctors_policies_ratecards_id": 1,
      "ratecard_activation_status": "Pending",
      "ratecard_id": 1,
      "ratecard_validity": "2017-04-17T13:16:58.000Z",
      "ratecard_details": "{'Root Canal': 300, 'Consultation': 500}",
      "onboarded_status": "Available"
    },
    {
      "doctor_id": "xxxxxx",
      "doctor_name": "<NAME>",
      "practice_id": "xxxxxx",
      "is_owner": 0,
      "policy_id": 2,
      "policy_name": "GOLD Super Gold",
      "payor_id": 1,
      "payor_name": "AMEX",
      "payor_registration_number": "AMEX",
      "payor_contact_email": "support@amex.com",
      "payor_support_number": "<PHONE>",
      "payor_logo_url": "url",
      "doctors_policies_ratecards_id": 2,
      "ratecard_activation_status": "Pending",
      "ratecard_id": 1,
      "ratecard_validity": "2017-04-17T13:16:58.000Z",
      "ratecard_details": "{'Root Canal': 300, 'Consultation': 500}",
      "onboarded_status": "Available"
    }
  ]
};

const payors = new Map();

function isPayorProperty(value, key) {
  return key.indexOf('payor') === 0;
}

for (const card of input.rate_cards) {
  if (!payors.has(card.payor_id)) {
    payors.set(card.payor_id, _.assign({
      data: []
    }, _.pickBy(card, isPayorProperty)));
  }

  const payor = payors.get(card.payor_id);
  payor.data.push(_.pickBy(card, _.negate(isPayorProperty)));
}

const output = {
  "rate_cards": []
};

for (const payor of payors) {
  output.rate_cards.push(payor[1]);
}

console.log(output);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

答案 2 :(得分:0)

在普通的Javascript中,您可以使用哈希表作为对同一分组项的引用,并使用查找分隔数据(如果前缀为payor),并将其中的一个用于分组数据,其余用于相关数据。

var data = { rate_cards: [{ doctor_id: "xxxxxx", doctor_name: "Dr. Dre", practice_id: "xxxxx", is_owner: 0, policy_id: 1, policy_name: "GOLD Super Gold", payor_id: 1, payor_name: "AMEX", payor_registration_number: "AMEX123", payor_contact_email: "support@amex.com", payor_support_number: "PHONE", payor_logo_url: "url", doctors_policies_ratecards_id: 1, ratecard_activation_status: "Pending", ratecard_id: 1, ratecard_validity: "2017-04-17T13:16:58.000Z", ratecard_details: "{'Root Canal': 300, 'Consultation': 500}", onboarded_status: "Available" }, { doctor_id: "xxxxxx", doctor_name: "<NAME>", practice_id: "xxxxxx", is_owner: 0, policy_id: 2, policy_name: "GOLD Super Gold", payor_id: 1, payor_name: "AMEX", payor_registration_number: "AMEX", payor_contact_email: "support@amex.com", payor_support_number: "<PHONE>", payor_logo_url: "url", doctors_policies_ratecards_id: 2, ratecard_activation_status: "Pending", ratecard_id: 1, ratecard_validity: "2017-04-17T13:16:58.000Z", ratecard_details: "{'Root Canal': 300, 'Consultation': 500}", onboarded_status: "Available" }] },
    result = { payors: [] };

data.rate_cards.forEach(function (hash) {
    return function (o) {
        var temp = { payor: {}, data: {} };
        Object.keys(o).forEach(function (k) {
            temp[k.match(/^payor/) || 'data'][k] = o[k];
        });
        if (!hash[o.payor_name]) {
            temp.payor.data = [];
            hash[o.payor_name] = temp.payor;
            result.payors.push(hash[o.payor_name]);
        }
        hash[o.payor_name].data.push(temp.data);
    };
}(Object.create(null)));
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }