Handling Emojis in JSON and REST APIs Without Corruption
JSON handles emojis natively, but every layer between sender and receiver can break them. Here is how to ship emojis through an API stack intact. The first time I tried to send an emoji through a JSON API, the receiving end stored it as a string of question marks. The bug was not in JSON itself, which handles emojis cleanly, but in the chain of encoding assumptions between my code, the HTTP layer, and the database. Emojis expose every weak link in your text handling, and JSON APIs are where those links get stressed the most. Here is debugging this stack. The debugging took three days. I checked the JSON, which was correct. I checked the HTTP headers, which were wrong. I checked the database charset, which was also wrong. The emoji was being corrupted at two different points in the stack, and fixing one without the other did not solve the problem. I had to fix the entire chain before the emoji survived the round trip. JSON Handles Emojis Natively The JSON specification uses UTF-8 by default, and UTF-8 encodes all emojis including those above U+10000 without any special handling. A JSON document can contain an emoji directly in a string value, and any compliant parser will read it back correctly. The problems start when something between the sender and the receiver assumes a different encoding or truncates bytes. Read the full article on Emoji Reference, and copy any emoji mentioned from the catalog of 1303+ entries on the home page.