0

我正在尝试使用 JSONata 在以下 JSON 的“选项”数组中附​​加一个额外的对象:

{
  "description": "[IGNORE] Field used for testing",
  "displayOrder": 2,
  "fieldType": "select",
  "formField": true,
  "groupName": "excell_data",
  "label": "Dev Test Field [IGNORE]",
  "name": "dev_test_field",
  "options": [
    {
      "description": "Choice number one",
      "displayOrder": 1,
      "hidden": false,
      "label": "Option 1",
      "value": "1"
    },
    {
      "description": "Choice number two",
      "displayOrder": 2,
      "hidden": false,
      "label": "Option 2",
      "value": "2"
    },
    {
      "description": "Choice option three",
      "displayOrder": 3,
      "hidden": false,
      "label": "Option 3",
      "value": "3"
    }
  ],
  "type": "enumeration"
}

这样就变成了:

{
  "description": "[IGNORE] Field used for testing",
  "displayOrder": 2,
  "fieldType": "select",
  "formField": true,
  "groupName": "excell_data",
  "label": "Dev Test Field [IGNORE]",
  "name": "dev_test_field",
  "options": [
    {
      "description": "Choice number one",
      "displayOrder": 1,
      "hidden": false,
      "label": "Option 1",
      "value": "1"
    },
    {
      "description": "Choice number two",
      "displayOrder": 2,
      "hidden": false,
      "label": "Option 2",
      "value": "2"
    },
    {
      "description": "Choice option three",
      "displayOrder": 3,
      "hidden": false,
      "label": "Option 3",
      "value": "3"
    },
    {
      "description": "Choice number four",
      "displayOrder": 4,
      "hidden": false,
      "label": "Option 4",
      "value": 4
    }
  ],
  "type": "enumeration"
}

但是,当我尝试使用 append 函数时,我很难返回父 JSON 以及嵌套在其中的附加对象。

JSONata fiddler 链接在这里:https ://try.jsonata.org/iv7zhPZcr

谁能阐明我哪里出错了?

提前谢谢了。乔纳森

4

1 回答 1

1

使用变换运算符修改父对象中的选项对象:

$ ~> | $ | {
    "options": [options, {
        "description": "Choice number four",
        "displayOrder": 4,
        "hidden": false,
        "label": "Option 4",
        "value": 4
    }]
} | 

见:https ://try.jsonata.org/9RLURkd9l

https://docs.jsonata.org/other-operators#-------transform

于 2021-09-15T13:40:18.833 回答