如何使用Jolt Spec在嵌套的json中插入json对象

sd2nnvve  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(139)

我有一个用例,我需要将所有JSON对象放在嵌套的JSON数组中。我试着使用@来得到这个,但总是得到null。贴上了我正在使用的震动

输入:

{
  "status": "Pink",
  "summary": "violate",
  "type": "Image",
  "affectedPic": [
    {
      "PicType": "Nature",
      "name": "County-nature",
      "PicId": 4239
    },
    {
      "PicType": "Abstract",
      "name": "Buildings",
      "PicId": 1937
    },
    {
      "PicType": "Technology",
      "name": "AI",
      "PicId": 6937
    }
  ],
  "archived": true
}

预期输出:

{
  "affectedPic": [
    {
      "PicType": "Nature",
      "name": "County-nature",
      "PicId": 4239,
      "status": "Pink",
      "summary": "violate",
      "type": "Image",
      "archived": true
    },
    {
      "PicType": "Abstract",
      "name": "Buildings",
      "PicId": 1937,
      "status": "Pink",
      "summary": "violate",
      "type": "Image",
      "archived": true
    },
    {
      "PicType": "Technology",
      "name": "AI",
      "PicId": 6937,
      "status": "Pink",
      "summary": "violate",
      "type": "Image",
      "archived": true
    }
  ]
}

My Jolt:

[
  {
    "operation": "shift",
    "spec": {
      "status": "@(1,affectedPic)",
      "summary": "@(1,affectedPic)",
      "type": "@(1,affectedPic)",
      "archived": "@(1,affectedPic)"
    }
  }
]

我已经在JOLT规范中应用了多种组合,但它不起作用,请建议。

kuarbcqp

kuarbcqp1#

你可以用

[
  {
    "operation": "shift",
    "spec": {
      "affectedPic": {
        "*": {
          "@(2,status)": "[#2].status", // go two levels up to grab the value of 
                                        // the attribute at the level of 
                                        // the "affectedPic" array
                                        // generate array-wise results by [#2] after 
                                        // reaching the indexes level of the array
                                        // to start counting with 1 instead of 0  
          "@(2,summary)": "[#2].summary",
          "@(2,type)": "[#2].type",
          "*": "[#2].&",
          "@(2,archived)": "[#2].archived"
        }
      }
    }
  }
]

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

或者更动态地(不单独写入留在数组外部的属性):

[
  {
    "operation": "shift",
    "spec": {
      "*": "others.&",
      "affectedPic": "&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "affectedPic": {
        "*": {
          "@2,others": { "*": "[&1].&" },
          "*": "[&1].&" // generate array-wise results by [&1] to 
                                        // reach the indexes level of the array
                                        // to start counting with 0(as 0,1)
        }
      }
    }
  }
]

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

相关问题