Table of Contents
Frequently Asked Questions (FAQs) about JSON Server
What is the main purpose of using JSON Server?
How can I install JSON Server?
How can I create a custom route in JSON Server?
Can I use JSON Server for production?
How can I add data to my JSON Server?
How can I filter data in JSON Server?
Can I use JSON Server with other programming languages?
How can I paginate data in JSON Server?
Can I sort data in JSON Server?
How can I update data in JSON Server?
Home Web Front-end JS Tutorial JSON Server Example

JSON Server Example

Mar 09, 2025 am 12:33 AM

JSON Server Example

JSON Server Example

This JSON server example is part of an article series that was rewritten in mid 2017 with up-to-date information and fresh examples.

The JSON Server is a popular tool for front-end developers for quickly setting up a fully fake REST API in less than a minute. You need to first install it via npm:
<span>npm install -global json-server
</span>
Copy after login

Next, save some data in a JSON file and name it db.json:

<span>{
</span>  <span>"clients": [
</span>    <span>{
</span>      <span>"id": "59761c23b30d971669fb42ff",
</span>      <span>"isActive": true,
</span>      <span>"age": 36,
</span>      <span>"name": "Dunlap Hubbard",
</span>      <span>"gender": "male",
</span>      <span>"company": "CEDWARD",
</span>      <span>"email": "dunlaphubbard@cedward.com",
</span>      <span>"phone": "+1 (890) 543-2508",
</span>      <span>"address": "169 Rutledge Street, Konterra, Northern Mariana Islands, 8551"
</span>    <span>},
</span>    <span>{
</span>      <span>"id": "59761c233d8d0f92a6b0570d",
</span>      <span>"isActive": true,
</span>      <span>"age": 24,
</span>      <span>"name": "Kirsten Sellers",
</span>      <span>"gender": "female",
</span>      <span>"company": "EMERGENT",
</span>      <span>"email": "kirstensellers@emergent.com",
</span>      <span>"phone": "+1 (831) 564-2190",
</span>      <span>"address": "886 Gallatin Place, Fannett, Arkansas, 4656"
</span>    <span>},
</span>    <span>{
</span>      <span>"id": "59761c23fcb6254b1a06dad5",
</span>      <span>"isActive": true,
</span>      <span>"age": 30,
</span>      <span>"name": "Acosta Robbins",
</span>      <span>"gender": "male",
</span>      <span>"company": "ORGANICA",
</span>      <span>"email": "acostarobbins@organica.com",
</span>      <span>"phone": "+1 (882) 441-3367",
</span>      <span>"address": "697 Linden Boulevard, Sattley, Idaho, 1035"
</span>    <span>}
</span>  <span>]
</span><span>}
</span>
Copy after login

Finally, start the server with the following command:

json-server <span>--watch src/db.json
</span>
Copy after login

You can now access the simple REST API with a suitable client. For now, a modern browser like Chrome, Firefox or Safari will do. Open http://localhost:3000/clients and you will see your entire miniature database in JSON format. You can view items by id by using the request format http://localhost:3000/clients/{id}. For example, opening http://localhost:3000/clients/59761c233d8d0f92a6b0570d will yield:

<span>{
</span>  <span>"id": "59761c233d8d0f92a6b0570d",
</span>  <span>"isActive": true,
</span>  <span>"age": 24,
</span>  <span>"name": "Kirsten Sellers",
</span>  <span>"gender": "female",
</span>  <span>"company": "EMERGENT",
</span>  <span>"email": "kirstensellers@emergent.com",
</span>  <span>"phone": "+1 (831) 564-2190",
</span>  <span>"address": "886 Gallatin Place, Fannett, Arkansas, 4656"
</span><span>}
</span>
Copy after login

To learn more about the JSON server, check out the tutorial Mock REST APIs Using json-server

Also: See more JSON examples.

Here are the other examples in this series:
  • Colors JSON Example
  • Google Maps JSON Example
  • YouTube JSON Example
  • Twitter JSON Example
  • GeoIP JSON Example
  • WordPress JSON Example
  • Database JSON Example
  • Local REST JSON Example
  • Test Data JSON Example

Frequently Asked Questions (FAQs) about JSON Server

What is the main purpose of using JSON Server?

JSON Server is a simple tool primarily used for setting up a fake REST API for development purposes. It allows developers to prototype and develop applications without having to set up a complex backend. This is particularly useful when you want to quickly test your front-end code with a mock backend. It uses a JSON file to create the database and provides all the standard REST API endpoints out of the box.

How can I install JSON Server?

JSON Server is a Node.js module, and it can be installed using npm (Node Package Manager). You can install it globally on your system by running the command npm install -g json-server in your terminal or command prompt. Once installed, you can start the server with the command json-server --watch db.json, where db.json is your database file.

How can I create a custom route in JSON Server?

JSON Server allows you to define custom routes by creating a routes.json file. In this file, you can map routes to different JSON objects. For example, if you want to map /api/posts to /posts, you would define it as {"/api/posts": "/posts"} in your routes.json file. Then, you can start the server with the routes file by running json-server --watch db.json --routes routes.json.

Can I use JSON Server for production?

While JSON Server is a powerful tool for prototyping and development, it is not recommended for production use. It lacks the security and performance optimizations necessary for a production environment. For production, you should use a proper database and server setup.

How can I add data to my JSON Server?

You can add data to your JSON Server by modifying the db.json file. This file acts as your database, and each key in the JSON object corresponds to a different resource. For example, if you want to add a new post, you would add a new object to the posts array in your db.json file.

How can I filter data in JSON Server?

JSON Server supports filtering data using query parameters. For example, if you want to get all posts with the title “Hello World”, you would send a GET request to /posts?title=Hello World. This will return all posts where the title is “Hello World”.

Can I use JSON Server with other programming languages?

Yes, JSON Server is language-agnostic and can be used with any programming language that can send HTTP requests. This includes JavaScript, Python, Ruby, Java, and many others.

How can I paginate data in JSON Server?

JSON Server supports pagination using the _page and _limit query parameters. For example, if you want to get the first 10 posts, you would send a GET request to /posts?_page=1&_limit=10. This will return the first 10 posts.

Can I sort data in JSON Server?

Yes, JSON Server supports sorting data using the _sort and _order query parameters. For example, if you want to get posts sorted by title in ascending order, you would send a GET request to /posts?_sort=title&_order=asc.

How can I update data in JSON Server?

You can update data in JSON Server by sending a PUT or PATCH request to the resource’s URL. For example, if you want to update the title of a post, you would send a PUT or PATCH request to /posts/1, where 1 is the id of the post, with the new title in the request body.

The above is the detailed content of JSON Server Example. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What should I do if I encounter garbled code printing for front-end thermal paper receipts? What should I do if I encounter garbled code printing for front-end thermal paper receipts? Apr 04, 2025 pm 02:42 PM

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

Demystifying JavaScript: What It Does and Why It Matters Demystifying JavaScript: What It Does and Why It Matters Apr 09, 2025 am 12:07 AM

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

Who gets paid more Python or JavaScript? Who gets paid more Python or JavaScript? Apr 04, 2025 am 12:09 AM

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

How to merge array elements with the same ID into one object using JavaScript? How to merge array elements with the same ID into one object using JavaScript? Apr 04, 2025 pm 05:09 PM

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Is JavaScript hard to learn? Is JavaScript hard to learn? Apr 03, 2025 am 12:20 AM

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

How to achieve parallax scrolling and element animation effects, like Shiseido's official website?
or:
How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? How to achieve parallax scrolling and element animation effects, like Shiseido's official website? or: How can we achieve the animation effect accompanied by page scrolling like Shiseido's official website? Apr 04, 2025 pm 05:36 PM

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

The difference in console.log output result: Why are the two calls different? The difference in console.log output result: Why are the two calls different? Apr 04, 2025 pm 05:12 PM

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

The Evolution of JavaScript: Current Trends and Future Prospects The Evolution of JavaScript: Current Trends and Future Prospects Apr 10, 2025 am 09:33 AM

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.

See all articles