Are multi-line strings allowed in JSON?

JSON does not allow real line-breaks. You need to replace all the line breaks with \n. eg: “first line second line” can saved with: “first line\nsecond line” Note: for Python, this should be written as: “first line\\nsecond line” where \\ is for escaping the backslash, otherwise python will treat \n as the control character “new … Read more

Use of PUT vs PATCH methods in REST API real life scenarios

NOTE: When I first spent time reading about REST, idempotence was a confusing concept to try to get right. I still didn’t get it quite right in my original answer, as further comments (and Jason Hoetger’s answer) have shown. For a while, I have resisted updating this answer extensively, to avoid effectively plagiarizing Jason, but … Read more

Parsing JSON with Unix tools

There are a number of tools specifically designed for the purpose of manipulating JSON from the command line, and will be a lot easier and more reliable than doing it with Awk, such as jq: curl -s ‘https://api.github.com/users/lambda’ | jq -r ‘.name’ You can also do this with tools that are likely already installed on … Read more

How do I get ASP.NET Web API to return JSON instead of XML using Chrome?

Note: Read the comments of this answer, it can produce a XSS Vulnerability if you are using the default error handing of WebAPI I just add the following in App_Start / WebApiConfig.cs class in my MVC Web API project. config.Formatters.JsonFormatter.SupportedMediaTypes .Add(new MediaTypeHeaderValue(“text/html”) ); That makes sure you get JSON on most queries, but you can … Read more

How to reformat JSON in Notepad++?

Update: As of Notepad++ v7.6, use Plugin Admin to install JSTool per this answer INSTALL Download it from http://sourceforge.net/projects/jsminnpp/ and copy JSMinNpp.dll to plugin directory of Notepad++. Or you can just install “JSTool” from Plugin Manager in Notepad++. New Notepad++ install and where did PluginManager go? See How to view Plugin Manager in Notepad++ { … Read more

How do I POST JSON data with cURL?

You need to set your content-type to application/json. But -d (or –data) sends the Content-Type application/x-www-form-urlencoded, which is not accepted on Spring’s side. Looking at the curl man page, I think you can use -H (or –header): -H “Content-Type: application/json” Full example: curl –header “Content-Type: application/json” \ –request POST \ –data ‘{“username”:”xyz”,”password”:”xyz”}’ \ http://localhost:3000/api/login (-H … Read more

Can comments be used in JSON?

No. JSON is data-only. If you include a comment, then it must be data too. You could have a designated data element called “_comment” (or something) that should be ignored by apps that use the JSON data. You would probably be better having the comment in the processes that generates/receives the JSON, as they are … Read more

Which JSON content type do I use?

For JSON text: application/json The MIME media type for JSON text is application/json. The default encoding is UTF-8. (Source: RFC 4627) For JSONP (runnable JavaScript) with callback: application/javascript Here are some blog posts that were mentioned in the relevant comments: Why you shouldn’t use text/html for JSON Internet Explorer sometimes has issues with application/json A rather … Read more