'Jolt Transform complex json to a flat json

I am looking to Jolt Transform a complex json into below desired json.

Input JSON:

{
  "Rating": 1,
  "SecondaryRatings": [
    {
      "Design": 4,
      "Price": 2,
      "RatingDimension3": 1,
      "Arr1": [
        {
          "Val1": 34
        },
        {
          "Val2": 45
        }
      ]
    },
    {
      "Design": 44,
      "Price": 23,
      "RatingDimension3": 12,
      "Arr1": [
        {
          "Val1": 56
        },
        {
          "Val2": 22
        }
      ]
    }
  ]
}

Desired Output

[
  {
    "Design": 4,
    "Price": 2,
    "RatingDimension3": 1,
    "rating-primary": 1,
    "Val1": 34,
    "Val2": 45
  },
  {
    "Design": 44,
    "Price": 23,
    "RatingDimension3": 12,
    "rating-primary": 1,
    "Val1": 56,
    "Val2": 22
  }
]

I tried following Jolt Spec

[
  {
    "operation": "shift",
    "spec": {
      "SecondaryRatings": {
        "*": {
          "*": {
            "*": {
              "@(2,Design)": "[&3].Design",
              "@(2,Price)": "[&3].Price",
              "@(2,RatingDimension3)": "[&3].RatingDimension3",
              "Val1": "[&3].Val1",
              "Val2": "[&3].Val2",
              "@(4,Rating)": "[&3].rating-primary"
            }
          }
        }
      }
    }
  }
]

But got following Output

[ 
 {
   "Design" : [ 4, 4, 4, 4, 4 ],
   "Price" : [ 2, 2, 2, 2, 2 ],
   "RatingDimension3" : [ 1, 1, 1, 1, 1 ],
   "rating-primary" : [ 1, 1, 1, 1, 1 ],
   "Val1" : 34,
   "Val2" : 45
 }, 
 {
   "Design" : [ 44, 44, 44, 44, 44 ],
   "Price" : [ 23, 23, 23, 23, 23 ],
   "RatingDimension3" : [ 12, 12, 12, 12, 12 ],
   "rating-primary" : [ 1, 1, 1, 1, 1 ],
   "Val1" : 56,
   "Val2" : 22
  } 
]

So as it is seen except for the last level values, all others are having array with repeated values. Can anybody help to what is missing or wrong in Jolt Spec?



Solution 1:[1]

You can directly walk through indexes of the outer array(SecondaryRatings) rather than inner one(Arr1) such as

[
  {
    "operation": "shift",
    "spec": {
      "SecondaryRatings": {
        "*": {
          "*": "[&1].&",
          "@(2,Rating)": "[&].rating-primary",
          "Arr1": {
            "*": {
              "*": "[&3].&"
            }
          }
        }
      }
    }
  }
]

in this case, no need to write all elements individually except for Rating

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barbaros Özhan