Many developers have found our previous blog, "Mocking an API in ONE Minute," useful, and you might too. That blog covered a quick way to mock an API, but it only scratched the surface. As I dug deeper into Apidog, I uncovered additional features that make mocking even complex data faster.
Handling APIs with Complex JSON Data
Let’s start with how to import a Swagger API into Apidog to mock JSON data. This way, you can generate mock data without writing any scripts.
However, some fields can be tricky to mock without additional configuration. For instance:
- Enum fields with multiple possible values, often used for status fields.
- Fixed-length arrays, like a table where each row has several attributes and a page might display 20 or 50 rows.
- Objects that reference schemas, as allowed by the OpenAPI spec, where an object or part of an object refers to a schema.
Fortunately, with Apidog, you can mock these complex JSON fields in just two minutes.
How to Mock APIs with Complexed JSON Data Quickly?
Here’s a simple three-step process to mock complex APIs using Apidog:
- Import API definitions.
- Customize API mock rules for specific fields.
- Get the mock URL.
It’s that easy. Let’s go through it.
Step 1: Import API Definitions
You only need an OpenAPI JSON or YAML file. Import it into Apidog, and you're ready to go.
Step 2: Customize API Mock Rules for Specific Fields
Enum values:
In our sample API, the 'status' field has three options: available, pending, and sold. We want our mock data to reflect only one of these values.
Click 'Advanced settings' in the 'Status' field, enable the 'Enum' switch, and add the enum values. Now, this field will only mock one of these values.
Fixed-length arrays:
For the 'PhotoUrls' array field, we need exactly five items.
In the 'Advanced settings' of the 'PhotoUrls' field, adjust the 'Element Limit' settings. Set both 'minItems' and 'maxItems' to 5.
Schema-referenced objects:
Apidog support schemas as per the OpenAPI specification. If an object refers to a schema, Apidog will mock it accordingly. For example, you can reference the schema for the 'Category' field.
Step 3: Get the API Mock URL
Click ‘Save', and switch to the 'Endpoint' tab. Copy the URL under the "Local Mock" section, which starts with "127.0.0.1".
Paste the URL into your browser, and you're done.
Notable Fields
When you check the mocked data, you’ll notice:
- PhotoUrls has exactly 5 items, all with valid image URLs.
- Status is limited to one of the enum values (sold, pending, or available).
- Category refers to the correct schema.
Other fields, like city and createdAt, are mocked automatically.
And if you refresh the URL, the mock data updates.
Apidog Supports Mocking JSON Schema for APIs
Apidog is fully compatible with OpenAPI 3.0, which uses JSON Schema to define the structure of API data. All JSON Schema attributes are supported in Apidog, including:
- String length range, format, and pattern (regex)
- Minimum and maximum integer values
- Read-only/write-only settings
Not just JSON, Apidog also supports mocking XML. You can even customize the XML tag names and prefixes.
Thanks to JSON Schema, you can generate any complex mock data with just a few clicks using Apidog—no coding required.