简体   繁体   中英

Jolt Transformation: How to shift some values into each map in list

I have been working on Jolt transformation from yesterday and unable to find a solution to achieve following output.
I would like to insert "year","month","day" into each map (dictionary) of "records" list.

Input

[{"root":
   {"body":
    {"year":2019,
     "month":8,
     "day":9,
     "records":[
       {"user":"a",
        "item":"x",
        "price":300,
        "count":1},
       {"user":"b",
        "item":"y",
        "price":100,
        "count":3}]
     }
   }
}]

Desired Output

[{"user":"a",
  "item":"x",
  "price":300,
  "count":1,
  "year":2019,
  "month":8,
  "day":9},
  {"user":"b",
   "item":"y",
   "price":100,
   "count":3,
   "year":2019,
   "month":8,
   "day":9}
]

I could insert value of "year", "month","day" into outside of each map as list like {"record":[[2019,8,9],{map1},{map2}]}, but this is not what I want.

I appreciate for any help or advice. And thank you in advance.

This should do what you want:

Solution (Explanation inline):

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "root": {
          "body": {
            "records": {
              //match records array
              "*": {
                //copy object user, item etc. to current position
                "@": "[&1]",
                //go up two levels and get year and place it in current array position
                "@(2,year)": "[&1].year",
                "@(2,month)": "[&1].month",
                "@(2,day)": "[&1].day"
              }
            }
          }
        }
      }
    }
  }
]

Output:

[
  {
    "user": "a",
    "item": "x",
    "price": 300,
    "count": 1,
    "year": 2019,
    "month": 8,
    "day": 9
  },
  {
    "user": "b",
    "item": "y",
    "price": 100,
    "count": 3,
    "year": 2019,
    "month": 8,
    "day": 9
  }
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM