I have managed to convert CSV data to JSON using the csvtojson npm package and successfully display it in the console.
csvtojson()
.fromFile(csvFilePath)
.then(jsonObj => {
console.log(jsonObj);
})
[
{
id: '1',
title: 'Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops',
price: '109.95',
description: 'Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday',
category: 'men clothing',
image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg'
}
]
Now I want to console log only the id and title field, how should I go about it? Many thanks in advance and greatly appreciate any helps. Thanks again
The reason is: It is an array of objects from the console.log values. You can do this:
console.log(`Id: ${jsonObj[0].id}` and Title: ${jsonObj[0].title})
If you have multiple objects, you can loop through it like this:
const jsonObj = [{
id: '1',
title: 'Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops',
price: '109.95',
description: 'Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday',
category: 'men clothing',
image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg'
}, {
id: '2',
title: 'Fjallraven - Foldsack No. 1 Backpack, Fits 15 Laptops',
price: '109.95',
description: 'Your perfect pack for everyday use and walks in the forest. Stash your laptop (up to 15 inches) in the padded sleeve, your everyday',
category: 'men clothing',
image: 'https://fakestoreapi.com/img/81fPKd-2AYL._AC_SL1500_.jpg'
}]
jsonObj.forEach(el => console.log(`Id: ${el.id}, title: ${el.title}`));