How to print the json (pretty) response and also save as json

Sure @Timo_Kuisma1 very close to achieve what I want but only problem remaining is change in format. Please look into this or suggest me something how can I achieve this with json format.

this is how I achieve in js.

Imagine we have this object in JavaScript:

var obj = { name: “John”, age: 30, city: “New York” };

Use the JavaScript function JSON.stringify() to convert it into a string.

var myJSON = JSON.stringify(obj);

myJSON is now a string, and ready to be sent to a server:
{“name”:“John”,“age”:30,“city”:“New York”}

Not sure how we can achieve this in groovy or java please.