It is giving me correct output, but can you give something more optimized in accordance with the code.
As long as you're using Java's builder-based approach, it's always going to seem clunky compared to JS or PHP. There are some libs you can use to allow for all-at-once JSON::stringify, like Gson. I can't vouch for any of these.