

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to selectively retrieve value from json output JavaScript
We have the following data inside a json file data.json −
data.json
{ "names": [{ "name": "Ramesh", "readable": true }, { "name": "Suresh", "readable": false }, { "name": "Mahesh", "readable": true }, { "name": "Gourav", "readable": true }, { "name": "Mike", "readable": false } ] }
Our job is to create a function parseData that takes in the path to this file as one and only argument, reads this json file, and returns an sub array of names array where the property readable is true.
Now, let’s write the code for this, we will use the require module to fetch the json data and then return a filtered array like this −
Example
const path = "./data.json"; const parseData = (path) => { const data = require(path); //no need to parse the data as it is already parsed return data.names.filter(el => el.readable); } const results = parseData(path); console.log(results);
Output
The console output will be −
[ { name: 'Ramesh', readable: true }, { name: 'Mahesh', readable: true }, { name: 'Gourav', readable: true } ]
- Related Questions & Answers
- How to selectively retrieve value from json output - JavaScript
- Retrieve property value selectively from array of objects in JavaScript
- How to read/retrieve data from Database to JSON using JDBC?
- How to retrieve data from JSON file using jQuery and Ajax?
- Retrieve values from nested JSON array in MongoDB?
- How to generate JSON output using Python?
- Get value for key from nested JSON object in JavaScript
- How to retrieve a value from MongoDB by its key name?
- How to retrieve value from an edit box using Selenium webdriver?
- How to correctly get a value from a JSON PHP?
- How to read data from JSON array using JavaScript?
- How to convert MySQL DATETIME value to JSON format in JavaScript?
- Retrieve element from local storage in JavaScript?
- How to convert JSON text to JavaScript JSON object?
- How to retrieve the value from a table cell with TableModel in Java?
Advertisements