JOLT转换以基于数组类型过滤联系人类型

时间:2019-05-08 18:56:20

标签: json jolt

如何检查JSON数组中的条件。考虑以下代码,我需要根据联系人类型进行过滤。如果联系人类型包含“电话”,则应显示电话类型。

{
  "entities": [
    [
      {
        "contactType": [
          "phone","email"
        ],
        "type": "111",
        "data": "111"
      },
      {
        "contactType": [
          "email"
        ],
        "type": "222",
        "data": "222"
      }
    ],
    [
      {
        "contactType": [
          "skype","phone"
        ],
        "type": "333",
        "data": "333"
      },
      {
        "contactType": [
          "email"
        ],
        "type": "444",
        "data": "444"
      }
    ]
  ]
}

基于联系人类型,我需要输出,例如,对于联系人类型“ phone”,需要显示以下输出:

{
  "entities": [
    {
      "type": "111",
      "data": "111"
    },
    {
      "type": "333",
      "data": "333"
    }
  ]
}

震动规格

 {
    "operation": "shift",
    "spec": {
      "entities": {
        "*": {
          "*": "entities.[]"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "entities": {
        "*": {
          "contactType": {
            "*": {
              "phone": {
                "type": "entities[&1].type",
                "data": "entities[&1].data"
              }
            }
          }
        }
      }
    }
  }
]

首先,我将数组的数组合并为包含电话和电子邮件类型的单个数组。 从联系人类型的电子邮件或电话中,应将其过滤到单个数组中。

1 个答案:

答案 0 :(得分:0)

[
  {
    "operation": "shift",
    "spec": {
      "entities": {
        // collect all contact type and add it in one Array
        "*": {
          "*": "entities.[]"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "entities": {
        "*": {
          "contactType": {
            // loop thru each contactType for the phone type
            "*": {
              "phone": {
                "@3": "entities[]"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "entities": {
        // removing contactType or adding type and data only
        "*": {
          "type": "entities[&1].type",
          "data": "entities[&1].data"
        }
      }
    }
  }
]