I’m confused with which to use json/2 or render/3. I think json/2 is easy to use, and render/3 is easy to read. But I often see developers use render/3 more than json/2.
I use render/3 basically. I use json/2 when the data whose key is not atom but string.
I recently started working with elixir/phoenix so take my opinion with a pinch of salt.
I think it’s ok to use json/2 if you are returning simple json structure like {"message": "Error"} and use render/3 when you have to return complex json structure like { "data": { "name": "demo_user_1"}}
render does a lot of things - content-type negotiation (if you pass an atom for the template name), layouts (for text/html responses), and expects a corresponding render head in the view module.
json doesn’t do any of that, so it’s useful for cases where the data’s already in the right shape to be converted to JSON (or short enough, like in @sushant12’s example).