如何使用JOLT转换复杂的JSON嵌套数组?

时间:2019-05-09 14:03:32

标签: json jolt

我试图根据第二个嵌套数组中的值数量将嵌套数组转换为对象。我似乎无法获取值字段的数量并将其用作我的规格中的键。现在这是我输入的JSON文件:

{
 "meta": {
   "regId": "us",
   "cId": "SomeProduct",
   "weId": 15

 },
 "data": {
   "name": "R",
   "details": {
     "headers": [
       "id",
       "cityId",
       "cityName"

     ],
     "values": [
       [
         1539,
         17,
         "Moskow"
       ],
       [
         1539,
         17,
         "Berlin"
       ],
       [
        1539,
         17,
         "Vienna"
       ]
     ]
   }
 }
}

这是我想要的JSON输出:

[
    {"regId": "us",
        "cId": "SomeProduct",
        "weId": 15,
        "name":"R",
        "id":1539,
        "cityId":17,
        "cityName":Moskow
    },
    {"regId": "us",
        "cId": "SomeProduct",
        "weId": 15,
        "name":"R",
        "id":1540,
        "cityId":11,
        "cityName":Berlin
    },
    {"regId": "us",
        "cId": "SomeProduct",
        "weId": 15,
        "name":"R",
        "id":151,
        "cityId":18,
        "cityName":Vienna
    }
]

这是我到目前为止的规范

[
  {
    "operation": "shift",
    "spec": {
      "meta": {
        "*": "&"
      },
      "data": {
        "name": "&",
        "details": {
          "values": {
            "*": {

              "*": "@(3,headers[&])"
            }
          }
        }
      }
    }
  }
]

有人有类似的情况吗?

2 个答案:

答案 0 :(得分:0)

看起来我取得了进步,但仍然不好笑:

当前规格:

[
  {
    "operation": "shift",
    "spec": {
      "meta": {
        "*": "&"
      },
      "data": {
        "name": "&",
        "details": {
          "values": {
            "*": {
              "*": "&.@(3,headers[&1])",
              "*": "&1"
            }
          }
        }
      }
    }
  }
]

输出为:

{
  "regId" : "us",
  "cId" : "SomeProduct",
  "weId" : 15,
  "name" : "R",
  "0" : [ 1539, 17, "Moskow" ],
  "1" : [ 1540, 18, "Berlin" ],
  "2" : [ 1541, 19, "Vienna" ]
    }

这应该是一个嵌套的规范,而不是一个吗?

答案 1 :(得分:0)

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "details": {
          "values": {
            "*": {
              "*": {
                // @ takes value of each element and put it to a 
                // correspondent data2 element ([&2] - go up three levels to 
                // the first * and takes number of element)
                // @(4,headers[&0]) - go up 5 levels and take headers[&0] 
                // occurence (&0 - go up 1 level - second * and takes number 
                //of element)
                "@": "data2[&2].@(4,headers[&0])" 
              },
              // go up four levels and grab name value and put it into 
              // data2[&1].name
              "@(3,name)": "data2[&1].name",
              "@(4,meta)": { // go up five levels and grab meta value
                "*": "data2[&2].&" // for each value put it to data2[&2] as it is
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",  // second shift operation to modify result of this above
    "spec": {
      "data2": ""  // removing data2 header
    }
  }
]