Last Updated: January 05, 2019
· bjacog

Adding comments to JSON

There is no comment notation available in JSON. So in order not to break the response with a nasty print_r or echo, we can simply include the output we want as one of the objects/items of the JSON object.

Consider the snippet below:

        "monthly_formatted":"$     614.21",
        "weekly_formatted":"$ 141.39",
        "debug_comment":"select monthly_raw from payment_tables 
         where user_id = 999 and 
       some_other_debug_value = 7 limit 1"}

This is quite handy for debugging larger requests.
Obviously these shouldn't make it to production as they add to the amount of data returned by the request.

10 Responses
Add your response


Normally JSON is not typed but generated by functions/methods. Better give comment on those.

over 1 year ago ·

In this scenario, a comment in the code would not have helped me debug the response, I needed to see the query that was ran to generate the json. This way made it nice and readable in chrome's console and as a bonus it didn't mess up the user experience.

over 1 year ago ·

Watching video online is so much interesting everyone like to watch it and also try to convert the format but they not got success so here i am posting the link address of video converter http://myglobalclip.com with the helps of it you can easily convert the format of audio of online videos.

10 months ago ·

Thank you very much for sharing this very useful information, I am very happy to have found this information that I have been looking for. http://generalelectionsupdate.edublogs.org/

8 months ago ·

This is a great example since it's multiline, shows code/sql being treated as text, and obviously a user-chosen key so people don't think they must use 'debug_comment' and nothing else.as some sort of comment magic word. Thanks!!

7 months ago ·

Thank you for your sharing. Thanks to this article I can learn more things. Expand your knowledge and abilities. Actually the article is very practical. Thank you!

7 months ago ·

This is a great post. I like this topic. This site has lots of advantage. I found many interesting things from this site. It helps me in many ways. Thanks for posting this again. https://www.xing.com/profile/ashley_austen/portfolio

6 months ago ·

Buy cheap safety guardrails that are used mostly outside the playground and on the road to get traffic to stay away on the road. https://www.omegaindl.com/product-category/guard-rail/

6 months ago ·

One day her curiosity won against common sense and she decided to break down her boundaries. She left home and entered a forest after the sun went down. Lost and afraid in the new world http://geometrydash-game.com

2 months ago ·