Jolt 转换 JSON 规范,涉及未知键。

huangapple go评论80阅读模式
英文:

Jolt Transform JSON Spec with Unknown keys

问题

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "paramCollection": {
          "*": {
            "*": {
              "@(1,&)": "[&3].&"
            }
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "paramCollection": {
          "*": {
            "*": {
              "limit": ""
            }
          }
        }
      }
    }
  }
]
英文:

I want to transform below input json to output json using JOLT. The main problem here is in the list, I have to remove some fields whose root key (Param1, Param2, ...) will differ dynamically. I tried below spec, which didn't work.
Need help in this case.

Input JSON : [
  {
    "paramCollection": [
      {
        "Param1": {
          "value": 1,
          "limit": "10"
        }
      },
      {
        "Param2": {
          "value": 1,
          "limit": "20"
        }
      }
    ]
  }
]

Output JSON : [ {
  "paramCollection" : [ {
    "Param1" : {
      "value" : 1
    }
  }, {
    "Param2" : {
      "value" : 1
    }
  } ]
} ]

spec : [
  {
    "operation": "remove",
    "spec": {
      "*": {
        "paramCollection" : {
          "*": {
            "[&1].[&1].limit": ""
          }
        }
      }
    }
  }
]

答案1

得分: 1

你几乎是正确的。这是稍微修改过的规范,应该可以工作:

[
  {
    "operation": "remove",
    "spec": {
      "*": {
        "paramCollection": {
          "*": {
            "*": {
              "limit": ""
            }
          }
        }
      }
    }
  }
]

[&1].[&1]. 似乎是多余且不正确的。看起来 remove 操作 不支持 ampersand(&)通配符。请参阅 shift 操作文档
& 通配符

  • 适用于 LHS(左侧 - 输入 JSON 键)和 RHS(输出数据路径)
英文:

You are almost correct. This is the slightly modified spec which should work:

[
  {
    "operation": "remove",
    "spec": {
      "*": {
        "paramCollection": {
          "*": {
            "*": {
              "limit": ""
            }
          }
        }
      }
    }
  }
]

The [&1].[&1]. seems reduntant and incorrect. It seems that the remove operation does not support the apmersand (&) wildcard. See the shift operation docs
>'&' Wildcard
> * Valid on the LHS (left hand side - input JSON keys) and RHS (output data path)
>

huangapple
  • 本文由 发表于 2020年8月26日 17:50:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63595013.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定