--- tags: node.js, npm,json server, angular, sort, search title: Sort & Search in JSON Server description: json server node package sorting and searching db image: https://i.imgur.com/E0Ok1pT.jpg author: Rupesh Tiwari --- # Sort & Search in JSON Server ![](https://i.imgur.com/E0Ok1pT.jpg) The ***json-server*** is a JavaScript library to create testing REST API's in your **Angular**, **React** or any **Front-end/Back-end** **Node.js** Projects. [Json Server](https://www.npmjs.com/package/json-server) is my favorite node package whenever I want to quickly spawn my web api without writing a single line. In this article you will learn how can you **search** and **sort** your entries to json db. ## Json Server setup Run this script to install json-server: `npm i -D json-server` Add below script for running Json Server in watch mode so that whenever db.json is changed it will reload the db server: `server: json-server --watch server/db.json` ## Courses in db.json Create db.json and add below entry in my db.json. ```json= { "courses": [ { "id": 3, "title": "Angular", "details": "Create First App", "percentComplete": 0, "approved": true, "studentId": "2", "createDate": "2021-01-15T18:18:53.391Z" }, { "id": 4, "title": "RxJS", "details": "Learn Basic", "percentComplete": 0, "approved": true, "studentId": "3", "createDate": "2021-01-15T18:19:35.393Z" }, { "id": 5, "title": "JavaScript", "details": "Learn Basic", "percentComplete": 5, "approved": true, "studentId": "5", "createDate": "2021-01-15T18:19:53.784Z" } ] } ``` Run the server `npm run server` ![](https://i.imgur.com/prAR3ig.png) ## Searching in db.json In order to search by property use the property name and postfix by `_like`. Example if we want to search course by `title` then use ``?title_like=${titleToSearch}`` Here is the source code to search course. ```ts searchCourse(s: string) { if (s) { return this.httpClient .get(`http://localhost:3000/courses?title_like=${s.trim()}`) .pipe(tap((_) => console.log(`Searching for: ${s}`))); } else { return this.httpClient.get(`http://localhost:3000/courses`); } } ``` ## Sorting entries from db.json Suppose you want to show a list of course and wanted to show the latest course then you must sort the courses by createDate in descending order. For sorting in json server use `?_sort=NAMEOFPROPERTY&_order=desc` Example: I want to sort courses by recent course show first ![](https://i.imgur.com/TD5eQM3.png) Here is the source code to fetch latest courses. ```ts all() { return this.http.get<Course[]>(`${this.getUrl()}?_sort=createDate&_order=desc`); } ``` --- ## Do You Want to become full stack developer? :computer: If you want to become full stack developer and grow your carrier as Lead Developer/Architect. Consider subscribing to our full stack development training programs. We have monthly membership plans and you will get unlimited access to all of our video courses, slides, source code & Monthly video calls. - Please visit www.fullstackmaster.net/pro to subscribe to All Access PRO membership. - Please visit www.fullstackmaster.net/elite to subscribe to All Access ELITE membership. You will get everything from PRO plan. Additionally you will get access to monthly live Q&A video call with Rupesh and ask doubts and get more tips and tricks. >You bright future is waiting for you so visit today www.fullstackmaster.net and allow me to help you to board on your dream software architect/lead role. --- ### :sparkling_heart: Contact Details: Say :wave: to me! * Rupesh Tiwari * www.rupeshtiwari.com * :email: <fullstackmaster1@gmail.com> * Founder of www.fullstackmaster.net :mortar_board: * [<img src="https://i.imgur.com/9OCLciM.png" width="295" height="65">](http://www.fullstackmaster.net)