O
O
Olga Mironova2019-07-03 09:11:31
JavaScript
Olga Mironova, 2019-07-03 09:11:31

How to parse data from json to another component?

Good afternoon professionals! Help pliiiiz))
There is a component in which I need to pull out data from json.
- json file created at the root near src in create-react-app
- this file has the following structure:

{
  "elem" : [
     {"id": "1", "title": "elem1", "color": "red"},
     {"id": "2", "title": "elem2", "color": "blue"},
     {"id": "3", "title": "elem3", "color": "white"},
],
}

- further, I need to parse this Json into another component, I seem to understand that I need to do something like JSON.parse (elem), but sorry, I don’t understand how to do it ((
the question is how to import it into another component and parse it there to work with him further?? the question may be very simple for you, but I was already tormented ((
Thank you very much in advance)))

Answer the question

In order to leave comments, you need to log in

1 answer(s)
H
h88p, 2019-07-03
@olgamiro1990

???
Example:
Data.json

{
    "privet": "hi"
}

app.js
import React from 'react';
import Data from './Data';
import './App.css';

function App() {
  return (
    <div className="App">
          {Data['privet']}
    </div>
  );
}

export default App;

ps
Source

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question