Sorry for the inconvenience.
The problem was that my logic was working for few working types and sets of json.
So, it wasn't able to recognize your need. So now I have made the logic more robust, so it can handle wide range of json types.
Thanks for feedback. Have a Good Day!!