Parkicism Parkicism - 2 years ago 111
Javascript Question

How can I parse through local JSON file in React js?

How can I parse through a JSON file retrieving all its data and using it in my code?

I've tried importing the file and just tried console logging it, but all it does is print Object {}:

import jsonData from "./file.json";

This is what my file.json looks like:

"id": 1,
"gender": "Female",
"first_name": "Helen",
"last_name": "Nguyen",
"email": "[email protected]",
"ip_address": ""
}, {
"id": 2,
"gender": "Male",
"first_name": "Carlos",
"last_name": "Fowler",
"email": "[email protected]",
"ip_address": ""

I'd want to be able to access the first and last name of each component and print those on the website.

Answer Source
var data = require('../../file.json'); // forward slashes will depend on the file location

for(var i = 0; i < data.length; i++) {
    var obj = data[i];

    console.log("Name: " + obj.first_name + ", " + obj.last_name);

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download