4

Input:

{

//The input json object

}

Desired Output:

{

"Event1": "Value1",
"Event2": [
            // collection of json objects
          ],

"Event3": {
            //The input json object
          }

}

So basically the input json goes in the "Event3" of another json object wrapper.

This is my Spec:

[
  {
    "operation": "shift",
    "spec": {
      "@": "Event3"
    }
  },
  {
    "operation": "default",
    "spec": {
      "Event1": "Value1",
      "Event2": [
        // some objects
       ]
    }
  }
]

Now the problem is - the above spec is doing the transformation, but ordering of the objects are messed up, like this:

{
"Event3": {
            //The input json object
          },
          
"Event2": [
            // some objects
          ],
"Event1": "Value1"

}

Please suggest how should I fix this.

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Ray
  • 43
  • 3

3 Answers3

2

have you tried using the sort operation?

[
  {
    "operation": "sort",
    "spec": {
      "*": ""
    }
  }
]

Think that might do the trick for you ;)

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
Lemmerich
  • 1,222
  • 1
  • 10
  • 13
  • Thanks for answering. A small problem though - sort will arrange them in alphabetical order of the attribute names; here in my case, the names I used - Event1, Event2 .. are just for representation. My actual json has different attribute names. Any way to set the order by specifying the attribute names? – Ray Oct 01 '19 at 04:15
  • Dont think so. From jolts git page: https://github.com/bazaarvoice/jolt/blob/master/jolt-core/src/main/java/com/bazaarvoice/jolt/Sortr.java "The sort order is standard alphabetical ascending". – Lemmerich Oct 03 '19 at 18:46
1

Add another shift operation and just copy paste each field from input json to output json.

[
  {
    "operation": "shift",
    "spec": {
      "@": "Event3"
    }
    },
  {
    "operation": "default",
    "spec": {
      "Event1": "Value1",
      "Event2": [
        "ob1", "obj2"
        ]
    }
    },
  {
    "operation": "shift",
    "spec": {
      "Event1": "&", //same as "Event1":"Event1",
      "Event2": "&",
      "Event3": "&"
    }
    }
]
dynamo
  • 89
  • 2
  • 9
0

Try using another shift operation at the end to get them in the desired order.

[
  {
    "operation": "shift",
    "spec": {
      "@": "Event3"
    }
    },
  {
    "operation": "default",
    "spec": {
      "Event1": "Value1",
      "Event2": [
        "ob1", "obj2"
        ]
    }
    },
  {
    "operation": "shift",
    "spec": {
      "Event1": "Event1",
      "Event2": "Event2",
      "Event3": "Event3"
    }
    }
]
Udith Gunaratna
  • 2,091
  • 1
  • 13
  • 17