Print JSON format String

This was a recent interview question. Suppose we have given following string:

"{"id": "0001", "type": "donut","name": "Cake","ppu": 0.55, "batters":{"batter":[{ "id": "1001", "type": "Regular" },{ "id": "1002", "type": "Chocolate" }]},"topping":[{ "id": "5001", "type": "None" },{ "id": "5002", "type": "Glazed" }]}"
we need to print it in following order:

{
	"id": "0001",
	"type": "donut",
	"name": "Cake",
	"ppu": 0.55,
	"batters":
		{
			"batter":
				[
					{ "id": "1001", "type": "Regular" },
					{ "id": "1002", "type": "Chocolate" }
				]
		},
	"topping":
		[
			{ "id": "5001", "type": "None" },
			{ "id": "5002", "type": "Glazed" }
		]
}

Thanks, I updated the formatting. Much better this way. Two things I want to mention, we can assume the string as well formed. And what happens if the string is too long or too deep? Which strategy (stack or single count variable) will be better?

Sorry folks for the wrong format. I didn't realize before posting. Thanks to https://adobe.github.io/Spry/samples/data_region/JSONDataSetSample.html

Comments (27)