首页 > 解决方案 > 合并两个不同的json对象

问题描述

我有一个场景,我将两个 json 对象组合在一个表中,它们在表中存储为两个不同的行。

考虑到以下几点,形成了组合的 json 对象

如果标签存在于第二行,则考虑该标签的值,如果不存在,则考虑第一行的值。

在此处输入图像描述

仅考虑第一行中存在的标签。JSON数据如下:

第 1 行 JSON ----------

[
  {
    "key": "service",
    "type": "radio",
    "templateOptions": {
      "label": "Service",
      "options": [
        {
          "value": "noService",
          "label": "Do Nothing",
          "color": "green"
        },
        {
          "value": "spin",
          "label": "Spin Cutter",
          "color": "yellow"
        },
        {
          "value": "replace",
          "label": "Replace Cutter",
          "color": "red"
        }
      ]
    }
  },
  {
    "key": "wear",
    "type": "radio",
    "templateOptions": {
      "label": "Wear",
      "options": [
        {
          "value": "w1",
          "label": "Wear 1",
          "color": "orange"
        },
        {
          "value": "w2",
          "label": "Wear 2",
          "color": "#123456"
        },
        {
          "value": "w3",
          "label": "Wear 3",
          "color": "#BBBB00"
        }
      ]
    }
  }
]

第 2 行 JSON ----------

[
  {
    "key": "service",
    "templateOptions": {
      "label": "서비스",
      "options": [
        {
          "value": "noService",
          "label": "아무것도하지 마세요",
          "color": "#330000"
        },
        {
          "value": "spin",
          "label": "스핀 커터",
          "color": "#AA0000"
        },
        {
          "value": "replace",
          "label": "커터 교체",
          "color": "#FF0000"
        }
      ]
    }
  },
  {
    "key": "wear",
    "templateOptions": {
      "label": "입고 있다",
      "options": [
        {
          "value": "w2",
          "color": "#770000"
        },
        {
          "value": "w3",
          "label": "착용 3",
          "color": "#990000"
        },
        {
          "value": "w123",
          "label": "커도하`, color: ‘#440000"
        }
      ]
    }
  }
]

组合 JSON ----------

[
  {
    "key": "service",
    "type": "radio",
    "templateOptions": {
      "label": "서비스",
      "options": [
        {
          "value": "noService",
          "label": "아무것도하지 마세요",
          "color": "#330000"
        },
        {
          "value": "spin",
          "label": "스핀 커터",
          "color": "#AA0000"
        },
        {
          "value": "replace",
          "label": "커터 교체",
          "color": "#FF0000"
        }
      ]
    }
  },
  {
    "key": "wear",
    "type": "radio",
    "templateOptions": {
      "label": "입고 있다",
      "options": [
        {
          "value": "w1",
          "label": "Wear 1",
          "color": "orange"
        },
        {
          "value": "w2",
          "label": "Wear 2",
          "color": "#770000"
        },
        {
          "value": "w3",
          "label": "착용 3",
          "color": "#990000"
        }
      ]
    }
  }
]

标签: arrayspostgresqljsonb

解决方案


推荐阅读