Re-parent a JSON object using Jolt

雨燕双飞 提交于 2019-12-02 15:58:40

问题


I'd like to move a JSON object down one level using Jolt. Here's my input data :

{
    "rating": {
        "primary": {
            "value": 3,
            "max": 5
        },
        "quality": {
            "value": 3,
            "max": 7
        }
    }
}

I'm using the following spec file :

[
  {
    "operation": "shift",
    "spec": {
      "$": "NEWPARENT"
    }
  }
]

I'm currently producing this output :

{
  "NEWPARENT" : "root"
}

However, my goal is to produce :

{
  "NEWPARENT": {
    "rating": {
      "primary": {
        "value": 3,
        "max": 5
      },
      "quality": {
        "value": 3,
        "max": 7
      }
    }
  }
}

Can anyone help me create the proper spec file?


回答1:


Spec

[
  {
    "operation": "shift",
    "spec": {
      "@": "NEWPARENT"
    }
  }
]

FYI, the "root" you are seeing is an artifact of the way that Jolt deals with the fact that the top level JSON input can be either a List or a Map, and that you want to be able to Transform a List into a Map (and vice versa).

So, jolt "wraps" the input so that it is "always" transforming a Map that has a single key "root", with a value that is the input.



来源:https://stackoverflow.com/questions/40494231/re-parent-a-json-object-using-jolt

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!