Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Jolt- rearrange elements

avatar
Contributor

Hello,
I am using jolt to change the structure of a payload to another form, and it must be dynamic, especially the top level elements "test" which can be multiple. I have tried but not getting it. I would appreciate your kind help.

Here is my input json:

{
  "test1": "test output",
  "test2": "test output",
  "Details": {
    "0": {
      "id": "first",
      "name": "the first one"
    },
    "1": {
      "id": "second",
      "name": "the second one"
    }
  }
}

 

and my output json is :

{
  "Details" : [ {
    "test1" : "test output",
    "test2" : "test output",
    "id" : "first",
    "name" : "the first one"
  }, {
    "test1" : "test output",
    "test2" : "test output",
    "id" : "second",
    "name" : "the second one"
  } ]
}

 

my current jolt spec is:

[
  {
    "operation": "shift",
    "spec": {
      "Details": "Details",
      "*": "outer.&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Details": {
        "*": {
          "*": "Details[&1].&",
          "@(2,outer)": "Details[&1].outer"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Details": {
        "*": {
          "outer": {
            "*": "Details[&2].&"
          },
          "*": "Details[&1].&"
        }
      }
    }
  }
]


But I am not getting the desired output json as above.


 

1 ACCEPTED SOLUTION

avatar
Super Guru

Try the below spec. Again not sure if this is the most efficient way. You might need to rethink your strategy if you are dealing with a lot of data:

 

[
  {
    //package all test fields into an outer object
    "operation": "shift",
    "spec": {
      "test*": "outer.&",
      "Details": "Details"
    }
   },
  {
    // Insert each outer object into Details element 0,1,..
    "operation": "shift",
    "spec": {
      "Details": {
        "*": {
          "*": "Details.&1.&",
          "@(3,outer)": "Details.&1.outer"
        }
      }
    }
   },
  {
    //Bucket each outer object (test1, test2...) into each details element
    "operation": "shift",
    "spec": {
      "Details": {
        "*": {
          "*": "Details.&1.&",
          "outer": {
            "*": {
              "@": "Details.&3.&"
            }
          }
        }
      }
    }
   },
  {
    //package details into seperate element under an Array 
    "operation": "shift",
    "spec": {
      "Details": {
        "*": "[#1].Details.&"
      }
    }
   }
  ]

View solution in original post

6 REPLIES 6

avatar
Super Guru

Hi,

 

You have not specified the desired output, but would the following give you what you are looking for:

[
  {
    "operation": "shift",
    "spec": {
      "Details": {
        "*": {
          "*": "Details[#2].&"
        }
      },
      "*": "outer[].&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Details": "Details",
      "outer": {
        "*": {
          "*": "Details[&1].&"
        }
      }
    }
  }
]

if that helps please accept solution.

Thanks

avatar
Contributor

Thank you sir @SAMSAL .

Actually the desired output is

{
  "Details" : {
"0":{ "test1" : "test output", "test2" : "test output", "id" : "first", "name" : "the first one" }},
"Details":{
"1":{ "test1" : "test output", "test2" : "test output", "id" : "second", "name" : "the second one" }}
}

 

avatar
Super Guru

Im not sure what you specified is makes sense  because you would have two keys with the same name "Details" but different values. I assume what you want is this:

 

 

{
	"Details": {
		"0": {
			"test1": "test output",
			"test2": "test output",
			"id": "first",
			"name": "the first one"
		},
		"1": {
			"test1": "test output",
			"test2": "test output",
			"id": "second",
			"name": "the second one"
		}
	}
}

 

In this case the spec would be like this:

 

[
  {
    //package all test fields into an outer object
    "operation": "shift",
    "spec": {
      "test*": "outer.&",
      "Details": "Details"
    }
   },
  {
    // Insert each outer object into Details element 0,1,..
    "operation": "shift",
    "spec": {
      "Details": {
        "*": {
          "*": "Details.&1.&",
          "@(3,outer)": "Details.&1.outer"
        }
      }
    }
   },
  {
    //Bucket each outer object (test1, test2...) into each details element
    "operation": "shift",
    "spec": {
      "Details": {
        "*": {
          "*": "Details.&1.&",
          "outer": {
            "*": {
              "@": "Details.&3.&"
            }
          }
        }
      }
    }
   }
]

 

Not sure if this is the best way, if someone knows a better way please  provide your suggestion.

If that answers your question please accept solution.

Thanks

 

avatar
Contributor

I am very grateful for your help so far sir @SAMSAL.

Actually, i made a silly mistake concerning that key duplication.

I wanted to have something of this nature, that is separate objects each with "Details" topmost level.

  "Details" : {
   "0":{
    "test1" : "test output",
    "test2" : "test output",
    "id" : "first",
    "name" : "the first one"
  }}, 
    "Details":{
    "1":{
    "test1" : "test output",
    "test2" : "test output",
    "id" : "second",
    "name" : "the second one"
  }}

 

 

 

avatar
Super Guru

Try the below spec. Again not sure if this is the most efficient way. You might need to rethink your strategy if you are dealing with a lot of data:

 

[
  {
    //package all test fields into an outer object
    "operation": "shift",
    "spec": {
      "test*": "outer.&",
      "Details": "Details"
    }
   },
  {
    // Insert each outer object into Details element 0,1,..
    "operation": "shift",
    "spec": {
      "Details": {
        "*": {
          "*": "Details.&1.&",
          "@(3,outer)": "Details.&1.outer"
        }
      }
    }
   },
  {
    //Bucket each outer object (test1, test2...) into each details element
    "operation": "shift",
    "spec": {
      "Details": {
        "*": {
          "*": "Details.&1.&",
          "outer": {
            "*": {
              "@": "Details.&3.&"
            }
          }
        }
      }
    }
   },
  {
    //package details into seperate element under an Array 
    "operation": "shift",
    "spec": {
      "Details": {
        "*": "[#1].Details.&"
      }
    }
   }
  ]

avatar
Contributor

Thank you sir @SAMSAL .

I am very grateful, at the moment, your solution does the job.
Also, i would love to learn JoltTransformation from you.
Thanks once again.