jolt规范根据json的属性条件生成json?

问题描述 投票:0回答:1

我的输入 JSON :

{
  "user": {
    "firstName": "abc",
    "lastName": "xyz",
    "email": "[email protected]"
  },
  "selections": [
    {
      "id": "123",
      "person": "user",
      "attributes": [
        {
          "attributeId": "AA",
          "options": [
            "11"
          ]
        },
        {
          "attributeId": "BB",
          "options": [
            "22"
          ]
        },
        {
          "attributeId": "CC",
          "options": [
            "33"
          ]
        }
      ]
    }
  ]
}

我的预期输出:

{
  "firstName": "User firstName",
  "lastName": "User lastName",
  "username": "concatination of firstname and lastname",
  "isActive": true,
  "valueofBB": "22",
  "phone": "",
  "CCLinkedToUser": [
    {
      "id": "33",
      "default": true
    }
  ]
}

你能帮忙为此创建震动变换吗?

这样我只能获取前 2 个参数,而无法获取其他条件值。 BB 和 CC 值。这里需要一些帮助。

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "firstName": "firstName",
        "lastName": "lastName",
        "isActive": "true",
        "email": "email",
        "phone": "null"
      }
    }
  }
]
json jolt
1个回答
0
投票

您可以使用以下转换:

[
  {
    "operation": "shift",
    "spec": {
      "user": {
        "*Name": "&" // loop thruogh all the "user.*Name"
      },
      "#true": "isActive", // hardcoe by usng # wildcard
      "selections": {
        "*": {
          "attributes": {
            "*": {
              "*": "@1,attributeId.&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "username": "=concat( @(1,firstName),' ',@(1,lastName) )",
      "phone": "" //add an attribute with a default value
    }
  },
  { // tile as in the desired order
    "operation": "shift",
    "spec": {
      "firstName|lastName|username": "&",
      "isActive": "&",
      "BB": {
        "options": { "0": "valueof&2" } // &2 replicates 2 levels upper node, eg. "BB"
      },
      "phone": "&",
      "CC": {
        "options": { "0": "&2LinkedToUser[0].id" }, // &2 refers the literal "CC"
        "#true": "&1LinkedToUser[0].default"
      }
    }
  },
  {// convert all boolean-convertible values to booleans 
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=toBoolean",
      "CCLinkedToUser": {
        "*": {
          "*": "=toBoolean"
        }
      }
    }
  }
]

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.