API FUNDAMENTALS / LESSON 04
JSON Data Format.
The language of APIs

Loading the lesson…
Loading activities…
API FUNDAMENTALS / LESSON 04
The language of APIs

Loading the lesson…
Loading activities…
When APIs send data back and forth, they need a format that both computers AND humans can read. That format is JSON. It stands for JavaScript Object Notation (forget the name, nobody says it out loud). What matters is that JSON is simple, readable and used pretty much everywhere.
JSON reads a lot like plain English. Here's a simple example.
Yep, that's it. That's JSON. You can probably tell what it says just by looking at it... a person named Sarah who's 28 years old and lives in Seattle.
JSON is built on one simple idea called key-value pairs. Every piece of data has two parts.
A colon sits between them, like this.
The key always goes in quotes. The value depends on what type of data it is.
JSON can hold a handful of different data types.
Text goes in double quotes.
Numbers skip the quotes.
Just the words true or false.
For when there's no value at all.
Multiple values inside square brackets.
Another set of key-value pairs tucked inside curly braces.
Let's look at something more realistic. Here's what an API might send back for a product.
That one JSON object is holding a bunch of stuff.
id)name)price)inStock)colors)reviews)tags)APIs send back lists of things ALL the time. That's an array of objects, where each item in the list is its own object.
That's three users in a list, and each one has an id, name and email.
Contact lists like this one show up EVERYWHERE in CRM land. I even built a free lil tool called CRM Data Cleaner.
JSON is kinda picky. Here are the rules it cares about.
Keys and string values have to use double quotes. Single quotes won't fly.
This one's correct.
This one's wrong, and any JSON parser will reject it.
Every key-value pair gets a comma after it, except the last one.
Don't put a comma after the last item. This one trips people up constantly.
This one's wrong. See the comma hanging off the end of "Seattle"?
This one's right. Commas after "Sarah" and 28, and nothing after the last item.
(Fun lil wrinkle... JSON doesn't allow comments either. No // notes, nothing. That's why all the explaining lives up here in the text and not inside the code.)
Objects get wrapped in curly braces { }.
Arrays get wrapped in square brackets [ ].
When you call an API, the response comes back as JSON. Your app has to read that data and do something with it.
Say you ask a weather API for the forecast.
Your app can pluck out just the pieces it needs.
It works the other direction too. When you send data to an API (like creating a new user), you send it as JSON.
The API reads that JSON and knows exactly what to do with it.
"name": "value"[ ] for lists{ } for grouped dataNow you know how data gets formatted. But what happens when something goes WRONG? Next lesson is all about status codes, the little numbers that tell you whether your request worked or blew up...