Emoji in Cloudant documents 🤔

Emoji are everywhere, but can they survive in a JSON data store?

Glynn Bird
May 30, 2018 · 3 min read

Cloudant and Apache CouchDB databases store their data as JSON documents so there’s only a handful of data types to choose from: strings, booleans, numbers, objects and arrays.

Here’s a document representing a person in a JSON:

Long gone are the days when we were limited to the ASCII character set in our code. In recent years, the characters we use to form sentences have go a lot more colourful.

Image for post
Image for post
Emoji on MacBook Pro keyboard. Photo by iabzd on Unsplash

Let’s try remodelling our document by using Emoji:

Is this valid? Will Cloudant accept documents like this? It turns out we’re good to go!

Image for post
Image for post
Emoji in JSON documents in the Cloudant dashboard

Cloudant is happy to store any valid JSON containing Unicode strings. Sometimes the Emojis render differently across devices depending on the character set used.

Can I use an Emoji as a key field?

Yes you can:

Image for post
Image for post
😀Emoji used as a document’s key _id field

Can I use an Emoji as a document attribute?

Yes you can:

Image for post
Image for post
Emoji used as keys in a JSON object.

What about in Map functions?

In JavaScript map functions, values of fields containing Emoji can be tested and Emoji keys and values can be emitted:

Image for post
Image for post
The JavaScript used to define the “map” part of a MapReduce operation may contain Emoji

Can I query using Emoji?

Yes you can:

Image for post
Image for post
Cloudant Query selectors can use Emoji too.

Why would I want an Emoji in a JSON document?

Just because you can do something, doesn’t mean you should.

In the above examples, I’m using Emojis like an enumerated data type where the symbols represent one of a set of allowed data values

  • Nationality: 🇬🇧 🇫🇷 🇩🇪 🇨🇳 🇯🇵
  • Mood: 😀 🤔 😐 ☹
  • Weather: ☀ ⛅ 🌧 🌪

Another use-case is the storage of Twitter, Slack, SMS or other strings that happen to contain Emoji in a JSON database. The data will be indexed correctly and can be queried and aggregated like any other data.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store