【问题标题】:JOLT: Merge specific data from JSON array using id key and leave other arrays untouchJOLT:使用 id 键合并 JSON 数组中的特定数据,并保持其他数组不变
【发布时间】:2022-11-17 19:22:57
【问题描述】:

我以前遇到过将数据合并到另一个数据以避免重复并制作更清晰的 JSON 版本的问题。我在这里得到了一个解决方案,它在一段时间内非常有效,但在我获得更多排列在 JSON 中的信息后,事情变得有点棘手。

我有这个数组:

{
  "clubhouse": [
    {
      "id": "01",
      "statusId": "ok",
      "stateid": "2",
      "nationalities": [
        {
          "nationalityid": "1"
        },
        {
          "nationalityid": "2"
        },
        {
          "nationalityid": "3"
        }
      ],
      "TypeId": "3",
      "investors": [
        {
          "investor": {
            "id": "1234",
            "gender": "01"
          },
          "inamount": "1500000",
          "ratio": "12"
        }
      ]
    },
    {
      "id": "01",
      "statusId": "ok",
      "stateid": "2",
"nationalities": [
        {
          "nationalityid": "1"
        },
        {
          "nationalityid": "2"
        },
        {
          "nationalityid": "3"
        }
      ],
      "TypeId": "3",
      "investors": [
        {
          "investor": {
            "id": "4321",
            "gender": "02"
          },
          "inamount": "1700000",
          "ratio": "12"
        }
      ]
    },
    {
      "id": "02",
      "statusId": "ok",
      "stateid": "2",
"nationalities": [
        {
          "nationalityid": "3"
        },
        {
          "nationalityid": "4"
        },
        {
          "nationalityid": "5"
        }
      ],

      "TypeId": "3",
      "investors": [
        {
          "investor": {
            "id": "1333",
            "gender": "01"
          },
          "inamount": "1500000",
          "ratio": "12"
        }
      ]
    },
    {
      "id": "03",
      "statusId": "ok",
      "stateid": "5",

"nationalities": [
        {
          "nationalityid": "3"
        },
        {
          "nationalityid": "4"
        },
        {
          "nationalityid": "5"
        }
      ],

      "TypeId": "3",
      "investors": [
        {
          "investor": {
            "id": "", 
            "gender": ""
          },
          "inamount": "",
          "ratio": ""
        }
      ]
    },
    {
      "id": "02",
      "statusId": "ok",
      "stateid": "2",
"nationalities": [
        {
          "nationalityid": "3"
        },
        {
          "nationalityid": "4"
        },
        {
          "nationalityid": "5"
        }
      ],
      "TypeId": "3",
      "investors": [
        {
          "investor": {
            "id": "1334",
            "gender": "02"
          },
          "inamount": "1900000",
          "ratio": "12"
        }
      ]
    }
  ]
}

我正在使用这个 JOLT 但它不适用于国籍,因为它失去了他们所在的数组。

[
  {
   // group by "id" values to create separate objects 
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "@(1,id).&",
          "investors": {
            "*": {
              "*": {
                "@": "@(4,id).&3[&4].&" // &3 -> going 3 levels up to grab literal "investors", [&4] -> going 4 levels up the tree in order to reach the indexes of "clubhouse" array, & -> replicate the leaf node values for the current key-value pair
              }
            }
          }
        }
      }
    }
  },
  {
    // get rid of "null" values
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    // pick only the first components from the repeated values populated within the arrays 
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE",
        "investors": "MANY"
      }
    }
  },
  {
    // get rid of object labels
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

我需要得到的是这样的:

{
  "clubhouse": [
    {
      "id": "01",
      "statusId": "ok",
      "stateid": "2",
            "nationalities": [
        {
          "nationalityid": "1"
        },
        {
          "nationalityid": "2"
        },
        {
          "nationalityid": "3"
        }
      ],
      "TypeId": "3",
      "investors": [
        {
          "investor": {
            "id": "1234",
            "gender": "01"
          },
          "inamount": "1500000",
          "ratio": "12"
        },
        {
          "investor": {
            "id": "4321",
            "gender": "02"
          },
          "inamount": "1700000",
          "ratio": "12"
        }
      ]
    },
    {
      "id": "02",
      "statusId": "ok",
      "stateid": "2",
      "nationalities": [
        {
          "nationalityid": "3"
        },
        {
          "nationalityid": "4"
        },
        {
          "nationalityid": "5"
        }
      ],
      "TypeId": "3",
      "investors": [
        {
          "investor": {
            "id": "1333",
            "gender": "01"
          },
          "inamount": "1500000",
          "ratio": "12"
        },
        {
          "investor": {
            "id": "1334",
            "gender": "02"
          },
          "inamount": "1900000",
          "ratio": "12"
        }
      ]
    },
    {
      "id": "03",
      "statusId": "ok",
      "stateid": "5",
      "nationalities": [
        {
          "nationalityid": "3"
        },
        {
          "nationalityid": "4"
        },
        {
          "nationalityid": "5"
        }
      ],
      "TypeId": "3",
      "investors": [
        {
          "investor": {
            "id": "",
            "gender": ""
          },
          "inamount": "",
          "ratio": ""
        }
      ]
    }
  ]
}

【问题讨论】:

  • 是的,这是一个错字。
  • 问题是数组 nationalityid(打字错误 nationalitysid)为我带来了与投资者一样多的数组,而我只需要一次。

标签: arrays json jolt


【解决方案1】:

您可以重新排列第一个转移通过添加标记为 "nationalities" 的新对象进行转换,与标记为 "investors" 的现有对象相比,该对象的标识符减少了一级,并且现有基数如果其余规格保持原样,转换将只选择重复的相同"nationalities"数组中的第一个数组,例如下面的

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "@(1,id).&",
          "nationalities": {
            "*": {
              "@": "@(3,id).&2[&3][]"
            }
          },
          "investors": {
            "*": {
              "*": {
                "@": "@(4,id).&3[&4].&"
              }
            }
          }
        }
      }
    }
  },
  ...
]

【讨论】:

  • 我尝试了几次,当我有多个国籍时,它就像一个魅力,但如果我只有一个,数组 "nationalities": [{}] 就像这样, "nationalities":{} 并且平台拒绝它
  • "nationalities" 对象的值标识符的右端添加一个 [] 将处理 @Sinuers 的情况。刚刚编辑了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-26
  • 2022-10-30
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
相关资源
最近更新 更多