json 前闪后微调值

pod7payv  于 2023-05-19  发布在  其他
关注(0)|答案(1)|浏览(148)

源JSON:

{
  "accounts": [
    {
      "name": "accounts/189134493",
      "createTime": "2021-02-08T08:55:53.722Z",
      "updateTime": "2021-02-08T08:55:53.722Z"
    },
    {
      "name": "accounts/192303354",
      "createTime": "2021-03-16T14:22:04.865Z",
      "updateTime": "2021-03-16T14:22:04.865Z"
    }
  ],
  "nextPageToken": "APDpoXMhpjzh_oJqiqip6upnMH"
}

预期:

[
  {
    "id": "1891344",
    "createTime": "2021-02-08T08:55:53.722Z",
    "updateTime": "2021-02-08T08:55:53.722Z"
  },
  {
    "id": "1923033",
    "createTime": "2021-03-16T14:22:04.865Z",
    "updateTime": "2021-03-16T14:22:04.865Z"
  }
]

我只能通过简单的移位操作从accounts数组中解嵌套值:

"spec": {
  "*": {
    "*": {
      "*": "[&1].&"
    }
  }
}

现在我需要从name中获取值,并在accounts/之后获取值并将其存储为id

46scxncf

46scxncf1#

您可以在modify转换中连续使用***split***和***join***函数,例如

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "accounts": {
        "*": {
          "name_": "=split('accounts/',@(1,name))", // split the pieces by the provided literal "accounts/"
          "id": "=join('',@(1,name_))" // combine the components of the newly formed arrays to yield string type values
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "accounts": {
        "*": {
          "nam*": ""
        }
      },
      "nextPageToken": ""
    }
  }
]

网站http://jolt-demo.appspot.com/上的demo

编辑:如果name属性的值有多个斜杠,如注解示例所示

"name":"accounts/189134493/properties/123"

并希望提取最后一个斜线之后的部分,例如

"id":"123"

则更倾向于使用以下包含***lastElement***函数的规范:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "accounts": {
        "*": {
          "name_": "=split('/',@(1,name))",
          "id": "=lastElement(@(1,name_))"
        }
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "accounts": {
        "*": {
          "nam*": ""
        }
      },
      "nextPageToken": ""
    }
  }
]

相关问题