Menu

1 / 2

JSON Schema: How to Validate API Responses Before They Break Your App

DEV Community·Snappy Tools·6 months ago
#24OazWIf
#webdev#javascript#beginners#json#schema
Reading 0:00
15s threshold

If you've ever shipped a bug because an API returned null where you expected a string, you've already experienced the problem JSON Schema solves.

JSON Schema is a vocabulary that lets you describe the structure of your JSON data and validate it automatically. It's been around since 2009 and is now a draft standard under the IETF. If you work with APIs, configs, or any structured JSON, it's worth knowing.

What JSON Schema actually does

A JSON Schema is itself a JSON document that describes what valid JSON looks like. Here's a minimal example:

{ "$schema" : "http://json-schema.org/draft-07/schema#" , "type" : "object" , "required" : [ "id" , "email" ], "properties" : { "id" : { "type" : "integer" }, "email" : { "type" : "string" , "format" : "email" }, "age" : { "type" : "integer" , "minimum" : 0 , "maximum" : 150 } } }

Enter fullscreen mode

Exit fullscreen mode

This schema says: the JSON must be an object with id (integer) and email (string, email format) as required fields.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.