How to convert complicated JSON nested array with JOLT?

被刻印的时光 ゝ 提交于 2019-12-11 18:27:29

问题


I'm trying to convert nested arrays into objects depending on the number of values in the second nested array. I can't seem to get the number of the value fields and use that as a key in my spec. Now this is my input JSON file:

{
 "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"
       ]
     ]
   }
 }
}

This my desired JSON Output:

[
    {"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
    }
]

This is my spec so far

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

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

Did someone have a similar situation?


回答1:


it looks like I made a progress, but still not good enaugh:

current spec:

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

the output is:

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

Should this be a nested specification rather than one?




回答2:


[
  {
    "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
    }
  }
]


来源:https://stackoverflow.com/questions/56061222/how-to-convert-complicated-json-nested-array-with-jolt

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