How to perform Jolt Spec operation in nested Json

Viewed 60

I am trying to write a Jolt Spec on nested json of following input record. There is a challenge in this...objectname is not always have multiple object like below, in some case it contain single object, and we have to parse this too.

Input

{
  "Status": "Green",
  "objectname": {
    "LED TV": {
      "values": [
        [
          "one",
          "two",
          "three",
          "four"
        ],
        [
          0,
          0,
          0,
          "one"
        ],
        [
          0,
          "one",
          0,
          "two"
        ]
      ],
      "time": [
        1663331241000,
        1663330155000,
        1663328545000
      ]
    },
    "LED Bulb": {
      "values": [
        [
          "one",
          "two",
          "three",
          "four"
        ],
        [
          0,
          0,
          0,
          "one"
        ],
        [
          0,
          "one",
          0,
          "two"
        ]
      ],
      "time": [
        1663331241000,
        1663330155000,
        1663328545000
      ]
    },
    "LED LAMP": {
      "values": [
        [
          "one",
          "two",
          "three",
          "four"
        ],
        [
          0,
          0,
          0,
          "one"
        ],
        [
          0,
          "one",
          0,
          "two"
        ]
      ],
      "time": [
        1663331541000,
        1663330555000,
        1663328545000
      ]
    }
  },
  "Source": "LED EQUIPS"
}

Expected Output

[
  [
    "Status",
    "Green",
    "objectname",
    "LED TV",
    "values",
    [
      "one",
      "two",
      "three",
      "four"
    ],
    [
      0,
      0,
      0,
      "one"
    ],
    [
      0,
      "one",
      0,
      "two"
    ]
  ],
  [
    "Status",
    "Green",
    "objectname",
    "LED Bulb",
    "values",
    [
      "one",
      "two",
      "three",
      "four"
    ],
    [
      0,
      0,
      0,
      "one"
    ],
    [
      0,
      "one",
      0,
      "two"
    ]
  ],
  [
    "Status",
    "Green",
    "objectname",
    "LED LAMP",
    "values",
    [
      "one",
      "two",
      "three",
      "four"
    ],
    [
      0,
      0,
      0,
      "one"
    ],
    [
      0,
      "one",
      0,
      "two"
    ]
  ],
  "Source",
  "LED EQUIPS"
]

PS : we have to parse "objectname" if they have one or multiple entries in it.

1 Answers

You can use $ wildcards to get keys of the attributes or objects for several layers, and use [# ] wildcards to generate respective arrays from the derived values from the nodes such as

[
  {
    "operation": "shift",
    "spec": {
      "o*": { // filter for "objectname"
        "*": {
          "v*": { // filter for "values"
            "#Status": "[#3]", // fixed value "Status" in order to repeat within the objects 
            "@(3,Status)": "[#3]", // pick the value of the Status attribute after going three levels up the tree
            "$2": "[#3]", // collect the key names for each object under related "objectname"
            "$1": "[#3]",
            "$": "[#3]",
            "*": {
              "@": "[#4]"
            }
          }
        }
      }
    }
  }
]
Related