我想使用 jolt 将一个输入 json 转换为所需的格式

I want to transform one input json to desired format using jolt

我的输入json

{
  "properties": [
    {
      "uniqueId": "100",
      "type": "boolean",
      "required": true,
      "definition": false,
      "description": "is bundled or not",
      "password": false,
      "name": "isBundled",
      "value": "true",
      "defaultValue": true,
      "hidden": false,
      "immutable": false,
      "isDeclaredListInput": false,
      "getInputProperty": false,
      "empty": false,
      "country": "asdsa"
    }
  ]
}

根据 defaultValue 是否存在,我们需要在 serviceSpecCharacteristicValue 中有一个字段 - isDefault - 如果存在值,则为 true,否则为 false

这是我的规格

[
  {
    "operation": "shift",
    "spec": {
      "properties": {
        "*": {
          "name": "serviceSpecCharacteristic[&1].name",
          "description": "serviceSpecCharacteristic[&1].description",
          "type": "serviceSpecCharacteristic[&1].valueType",
          "required": "serviceSpecCharacteristic[&1].required",
      "defaultValue":"serviceSpecCharacteristic[&1].serviceSpecCharacteristicValue[].value"
        }
      }
    }
  }
]

当前输出

{
  "serviceSpecCharacteristic" : [ {
    "name" : "isBundled",
    "description" : "is bundled or not",
    "valueType" : "boolean",
    "required" : true,
    "serviceSpecCharacteristicValue" : [ {
      "value" : "true"
    } ]
  } ]
}

预期输出:-根据 defaultValue 是否存在,我们需要在 serviceSpecCharacteristicValue 中有一个字段 – isDefault – 如果值存在则为 true,否则为 false

{
  "serviceSpecCharacteristic" : [ {
    "name" : "isBundled",
    "description" : "is bundled or not",
    "valueType" : "boolean",
    "required" : true,
    "serviceSpecCharacteristicValue" : [ {
      "value" : "true",
      "isdefault":true

    } ]
  } ]
}

如果原始 JSON 中不存在,则将默认值添加为 false。然后使用移位运算。

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "properties": {
        "*": {
          //set defaultValue to false if it does not exists.
          "defaultValue": "false"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "properties": {
        "*": {
          "name": "serviceSpecCharacteristic[&1].name",
          "description": "serviceSpecCharacteristic[&1].description",
          "type": "serviceSpecCharacteristic[&1].valueType",
          "required": "serviceSpecCharacteristic[&1].required",
          "value": "serviceSpecCharacteristic[&1].serviceSpecCharacteristicValue[&1].value",
          "defaultValue": "serviceSpecCharacteristic[&1].serviceSpecCharacteristicValue[&1].isdefault"
        }
      }
    }
  }
]

编辑 1 :

检查 defaultValue 是否为假,然后将 isdefault 设置为假,否则将 isdefault 设置为真。

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "properties": {
        "*": {
          //set defaultValue to false if it does not exists.
          "defaultValue": "false"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "properties": {
        "*": {
          "name": "serviceSpecCharacteristic[&1].name",
          "description": "serviceSpecCharacteristic[&1].description",
          "type": "serviceSpecCharacteristic[&1].valueType",
          "required": "serviceSpecCharacteristic[&1].required",
          "value": "serviceSpecCharacteristic[&1].serviceSpecCharacteristicValue[&1].value",
          "defaultValue": {
            "false": {
              "#false": "serviceSpecCharacteristic[&3].isdefault"
            },
            "*": { "#true": "serviceSpecCharacteristic[&3].serviceSpecCharacteristicValue[&3].isdefault" }
          }
        }
      }
    }
  }
]