简体   繁体   English

从两个 arrays 中提取元素以制作一个“平面”项目列表

[英]Pull elements out of two arrays to make a "flat" item list

I need to merge array data from two different arrays from the Input JSON and make a flat list of items in the Output JSON. The first array contains the key required for the output and the second array contains the value.我需要合并来自输入 JSON 的两个不同 arrays 的数组数据,并在 Output JSON 中制作一个平面项目列表。第一个数组包含 output 所需的键,第二个数组包含值。

So far, all my attempts at a spec haven't even come close, which is why I haven't listed one.到目前为止,我对规范的所有尝试都还没有完成,这就是为什么我没有列出一个。 Please see the Input and Desired Output below.请参阅下面的输入和所需的Output Thanks for any help!!谢谢你的帮助!!

Input JSON:输入 JSON:

{
  "data": [
    {
      "2": {
        "value": "DC1"
      },
      "3": {
        "value": 5
      }
    },
    {
      "2": {
        "value": "DC2"
      },
      "3": {
        "value": 10
      }
    }
  ],
  "fields": [
    {
      "id": 2,
      "label": "DataCenter",
      "type": "text"
    },
    {
      "id": 3,
      "label": "CCount",
      "type": "numeric"
    }
  ],
  "metadata": {
    "numFields": 2,
    "numRecords": 2,
    "skip": 0,
    "totalRecords": 2
  }
}

Desired Output JSON:所需 Output JSON:

[
  {
    "DataCenter": "DC1",
    "CCount": "5"
  },
  {
    "DataCenter": "DC2",
    "CCount": "10"
  }
]

You can use this spec:您可以使用此规范:

[
  {
    "operation": "shift",
    "spec": {
      "data": "&",
      "fields": {
        "*": {
          "label": "fields.@(1,id)"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "data": {
        "*": {
          "*": {
            "*": "[&2].@(4,fields.&1)"
          }
        }
      }
    }
  }
]

In your desired output CCount value is a string, You can add the below spec to change an integer to a string.在您想要的 output CCount值是一个字符串,您可以添加以下规范以将 integer 更改为字符串。

,
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "CCount": "=toString"
      }
    }
  }

在此处输入图像描述

You might reciprocally match id value of fields array vs. the object keys of the data array within a common object within the first shift transformation spec in order to prepare for the second one in which we tile the attributes into their individual objects such as您可以在第一个移位转换规范中的公共 object 中相互匹配fields数组的id值与data数组的 object 键,以便为第二个移位转换规范做准备,我们将属性平铺到它们的各个对象中,例如

[
  {
    "operation": "shift",
    "spec": {
      "*": { // represents the node for both "data" and "fields" arrays
        "*": {
          "*": {
            "value": "&1.&",
            "@1,label": "@2,id.&"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "value": {
          "*": "[&].@2,label"
        }
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is网站http://jolt-demo.appspot.com/上的演示

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM