JOLT 变换供参考

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

我正在寻找 JOLT 转换,以用其实体替换引用:

我的输入如下:

{
  "users": [
    {
      "id": 1,
      "firstname": "Max"
    },
    {
      "id": 4,
      "firstname": "Mel"
    }
  ],
  "special-role": {
    "id": 4
  }
}

我的预期输出应该是:

{
  "special-role" : {
     "firstname": "Mel"
  }
}

SO 上已经有一个示例关于映射参考的 JOLT 转换问题但我无论如何也无法找到解决问题的方法。我还没找到如何用

"@(2,users.4.firstname)": "&"
下的值替换
"special-role": { "id": 4 }

中的固定值“4”

我对规范的最新尝试是:

[
  {
    "operation": "shift",
    "spec": {
      "users": {
        "*": {
          "*": "&2.@(1,id).&"
        }
      },
      "special-role": "&"
    }
  },   
  {
    "operation": "shift",
    "spec": {
      "special-role": {
        "id": {
          "@(2,users.4.firstname)": "&"
        }
      }
    }
  }
]
reference mapping jolt
1个回答
0
投票
  • 第一层 RHS 上的最外层 ( &2 ) 转型是多余的
  • RHS 上的对象( &2 )的整数键可能会匹配 LHS 上的值
    special-role
    ( @(2,&) ) 在 第二次变身

所以,你可以使用:

[
  {
    "operation": "shift",
    "spec": {
      "users": {
        "*": {
          "fir*": "@(1,id).&" // pick the attribute "firstname" only
        }
      },
      "*": { // case of "special-role"
        "*": "&1"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@(2,&)": "&2"
        }
      }
    }
  }
]

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

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