当有2个嵌套数组时,展平复杂JSON

u1ehiz5o  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(138)

我遇到了一个问题,使用jolt转换转换一个非常复杂的嵌套JSON。输入和输出细节如下所示。
我试图创建一个震动规格,但没有得到它。有人可以请帮助我。详情如下:
输入JSON

{
  "key": [
    {
      "date": "27/09/2023"
    }
  ],
  "value": [
    {
      "values": [
        {
          "location": "3005",
          "total_capacity": 24,
          "capacity_flag": "",
          "orders": [
            {
              "order_type": "INIT",
              "consumed_capacity": 0
            },
            {
              "order_type": "PUSH",
              "consumed_capacity": 0
            }
          ]
        },
        {
          "location": "3007",
          "total_capacity": 72,
          "capacity_flag": "",
          "orders": [
            {
              "order_type": "INIT",
              "consumed_capacity": 0
            },
            {
              "order_type": "PUSH",
              "consumed_capacity": 0
            },
            {
              "order_type": "RPLN",
              "consumed_capacity": 0
            }
          ]
        }
      ]
    }
  ]
}

字符串
期望输出

[
  {
    "date": "27/09/2023",
    "location": "3005",
    "total_capacity": 24,
    "capacity_flag": "",
    "order_type": "INIT",
    "consumed_capacity": 0
  },
  {
    "date": "27/09/2023",
    "location": "3005",
    "total_capacity": 24,
    "capacity_flag": "",
    "order_type": "PUSH",
    "consumed_capacity": 0
  },
  {
    "date": "27/09/2023",
    "location": "3007",
    "total_capacity": 72,
    "capacity_flag": "",
    "order_type": "INIT",
    "consumed_capacity": 0
  },
  {
    "date": "27/09/2023",
    "location": "3007",
    "total_capacity": 72,
    "capacity_flag": "",
    "order_type": "PUSH",
    "consumed_capacity": 0
  },
  {
    "date": "27/09/2023",
    "location": "3007",
    "total_capacity": 72,
    "capacity_flag": "",
    "order_type": "RPLN",
    "consumed_capacity": 0
  }
]


我试过的震动规格,但日期不正确

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          "values": {
            "*": {
              "orders": {
                "*": {
                  "order_type": "&3.&1.order_type",
                  "consumed_capacity": "&3.&1.consumed_capacity",
                  "@2,location": "&3.&1.location",
                  "@2,total_capacity": "&3.&1.total_capacity",
                  "@2,capacity_flag": "&3.&1.capacity_flag"
                }
              }
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys generated within the previous spec
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]


需要帮助写一个震动规范,使我们得到一个扁平阵列

tyg4sfes

tyg4sfes1#

你只需要再次向上走,并拿起日期之前,包括它到您的输出。
这里有一个想法,你可以如何实现这一点(注意,我假设你将永远有一个日期)

[
  {
    "operation": "shift",
    "spec": {
      "value": {
        "*": {
          "values": {
            "*": {
              "orders": {
                "*": {
                  "@(6,key[0].date)": "&3.&1.date",
                  "order_type": "&3.&1.order_type",
                  "consumed_capacity": "&3.&1.consumed_capacity",
                  "@2,location": "&3.&1.location",
                  "@2,total_capacity": "&3.&1.total_capacity",
                  "@2,capacity_flag": "&3.&1.capacity_flag"
                }
              }
            }
          }
        }
      }
    }
  },
  { // get rid of the object keys generated within the previous spec
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

字符串

avkwfej4

avkwfej42#

一个更有活力和体面的选择可能是

[
  { // form "others" objects and "orders" arrays 
    // while extracting the value of the "date"
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "&",
          "values": {
            "*": {
              "*": "&1.others.&",
              "orders": {
                "*": {
                  "*": "&3.&2[&1].&"
                }
              }
            }
          }
        }
      }
    }
  },
  { // flatten the JSON by accumulating under the common values
    // through use of identifiers "&3_&1" and "&4_&1"
    // in order to derive them from the same level
    "operation": "shift",
    "spec": {
      "*": {
        "orders": {
          "*": {
            "@3,date": { "@": "&4_&1.date" },
            "@2,others": { "*": "&4_&1.&" },
            "*": "&3_&1.&"
          }
        }
      }
    }
  },
  { // get rid of the object keys generated within the previous spec
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

字符串
网站https://jolt-demo.appspot.com/上的 * 演示 * 是:


的数据

相关问题