Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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";
console.log(jsonData);

This is what my file.json looks like:

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

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

like image 600
Parkicism Avatar asked Jun 06 '16 04:06

Parkicism


People also ask

How do I read a local JSON file in React?

To load JSON data from local file into React, we can import them directly. import myData from "./data. json"; to import the JSON object in data.

How JSON parse data in React JS?

If you want to use the JSON data along with the key, then the parse() function can be used. The parse() function takes the argument of the JSON source and converts it to the JSON format, because most of the time when you fetch the data from the server the format of the response is the string.

How do I import a JSON file into React?

To import JSON file in React, we use import to import it like a regular module. import Profile from "./components/profile"; to import the ./components/profile JSON file as Profile . This works because the json-loader Webpack module is included with create-react-app .


2 Answers

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

var data = [
    {
      "id": 1,
      "gender": "Female",
      "first_name": "Helen",
      "last_name": "Nguyen",
      "email": "[email protected]",
      "ip_address": "227.211.25.18"
    }, {
      "id": 2,
      "gender": "Male",
      "first_name": "Carlos",
      "last_name": "Fowler",
      "email": "[email protected]",
      "ip_address": "214.248.201.11"
    }
];

for (var i = 0; i < data.length; i++)
{
    var obj = data[i];
    console.log(`Name: ${obj.last_name}, ${obj.first_name}`);
}

https://jsfiddle.net/c9wupvo6/

like image 129
fulvio Avatar answered Oct 18 '22 22:10

fulvio


Applications packaged with webpack 2.0.0+ (such as those created with create-react-app) support imports from json exactly as in the question (see this answer.

Be aware that import caches the result, even if that result is parsed json, so if you modify that object, other modules that also import it have references to the same object, not a newly parsed copy.

To get a "clean" copy, you can make a function that clones it such as:

import jsonData from './file.json';

const loadData = () => JSON.parse(JSON.stringify(jsonData));

Or if you're using lodash:

import jsonData from './file.json';
import { cloneDeep } from 'lodash';

const loadData = () => cloneDeep(jsonData);
like image 18
Eric Haynes Avatar answered Oct 18 '22 21:10

Eric Haynes