Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to use json object in ReactJS

Understanding JSON

Before diving into how to use JSON in ReactJS, let's first understand what JSON is. JSON stands for JavaScript Object Notation. It's a way to store information in an organized, easy-to-access manner. In a nutshell, it gives us a human-readable collection of data that we can access in a really logical manner.

Consider JSON as a notebook where we write down various aspects of different things. In each page of the notebook, we have details about one particular item. Similarly, JSON provides us with a structure to store data about various items.

JSON and JavaScript

Despite the name, JSON is a language-independent data format. It originated from JavaScript, but as of now, many programming languages include code to generate and parse JSON-format data. JSON is very similar to JavaScript's object literal notation and can be converted to a native JavaScript object using the JSON.parse method.

let jsonData = '{"name":"John", "age":30, "city":"London"}';
let obj = JSON.parse(jsonData);
console.log(obj.name); // Output: John

In the above example, we first defined a JSON object and then converted it into a JavaScript object. We then logged the 'name' from the object, which is 'John'.

JSON in ReactJS

Now that we have a basic understanding of JSON, let's move on to how we can use it in ReactJS. Since ReactJS is a JavaScript library, we can use JSON in React in a similar way as we use in JavaScript.

Fetching JSON Data

One common use case of JSON in ReactJS is fetching data from an API. Many APIs return data in JSON format, and React can easily handle this data format.

Let's take a look at an example where we are fetching data from an API that returns a JSON object.

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = { data: [] };
  }

  componentDidMount() {
    fetch('https://api.example.com/items')
      .then(response => response.json())
      .then(data => this.setState({ data }));
  }

  render() {
    return (
      <div>
        {this.state.data.map(item => (
          <p key={item.id}>{item.name}</p>
        ))}
      </div>
    );
  }
}

In the above example, we first created a class component with an initial state of data as an empty array. We then used the fetch function inside the componentDidMount lifecycle method to fetch JSON data from an API. The response from the API was then converted to a JavaScript object using the .json() method. The returned data was then set to the state using the setState method.

Passing JSON Data as Props

Another way to use JSON in ReactJS is to pass JSON data as props from a parent component to a child component.

class ParentComponent extends React.Component {
  render() {
    const data = { name: 'John', age: 30, city: 'London' };
    return <ChildComponent data={data} />;
  }
}

class ChildComponent extends React.Component {
  render() {
    const { data } = this.props;
    return (
      <div>
        <p>{data.name}</p>
        <p>{data.age}</p>
        <p>{data.city}</p>
      </div>
    );
  }
}

In the above example, we first created a ParentComponent which passes JSON data as a prop to the ChildComponent. The ChildComponent then destructures the data from the props and displays it.

Conclusion

Just like how a notebook helps us in organizing and accessing our notes, JSON helps us in organizing and accessing our data. It doesn't matter whether you're fetching it from an API or passing it around between components, JSON is a valuable tool in a React developer's toolbox. So next time when you're planning your data flow in React, think about how JSON can make your life easier. Remember, a well-organized notebook (or data) can save a lot of time!