How to Pass JSON Values into React Components ?
Last Updated :
18 Apr, 2024
In React, you can pass JSON values into components using props. Props allow us to send data from a parent component to its child components. When rendering a component, you can pass the JSON values as props. These props can then be accessed within the component’s function or class, allowing you to display the desired data.
Steps to Setup the Application:
Step 1:Â Create a reactJS application by using this command
npx create-react-app myapp
Step 2:Â Navigate to the project directory
cd myapp
Project Structure:

The updated dependencies in package.json file will look like:
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
}
Passing JSON values as props
Passing JSON values as props in React involves sending structured data objects from parent components to child components. This approach enables efficient data transfer and facilitates dynamic rendering, and helps developers to leverage JSON data for dynamic content display, state management, and application functionality within React components.
Example: In this example, the Account component receives the user prop containing the JSON data, which it can then use to display the user’s name and ID.
CSS
/*Account.css*/
.centered {
display: flex;
justify-content: center;
flex-direction: column;
align-items: center;
height: 100vh;
}
.green-text {
color: green;
}
JavaScript
// App.js
import React, { Component } from "react";
import Account from "./Account";
class App extends Component {
constructor(props) {
super(props);
this.state = {
user: {
id: 12,
firstName: "Bishal",
lastName: "Paul",
},
};
}
render() {
return (
<div>
<Account user={this.state.user} />
</div>
);
}
}
export default App;
JavaScript
// Account.jsx
import React from 'react'
import './Account.css'
const Account = (props) => {
const { user } = props
return (
<div className="centered">
<h1 className="green-text">
GeeksforGeeks
</h1>
<span>
Name - {user.firstName} {user.lastName}
</span>
<br />
<span>ID - {user.id}</span>
</div>
)
}
export default Account
Output:

Passing Array JSON
Passing arrays in JSON allows you to send multiple values in a single variable. JSON is a lightweight data-interchange format that is not user friendly for humans to read and write, and easy for machines to parse and generate.
Example: Implementation to show passing array JSON form one component to another.
JavaScript
// App.js
import React from 'react'
import ChildComponent from './ChildComponent.jsx'
const ParentComponent = () => {
const data = [
{ id: 1, name: 'Bishal' },
{ id: 2, name: 'Baishali' },
{ id: 3, name: 'Rahul' }
]
return (
<div style={{ textAlign: 'center', color: 'green' }}>
<h1>GeeksforGeeks</h1>
<p>Parent Component</p>
<ChildComponent data={data} />
</div>
)
}
export default ParentComponent
JavaScript
// ChildComponent.js
import React from 'react';
const ChildComponent = ({ data }) => {
const jsonData = JSON.stringify(data);
return (
<div>
<p>Child Component</p>
<pre>{jsonData}</pre>
</div>
);
};
export default ChildComponent;
Output:

Conclusion:
In React, passing JSON values as props is a common way to send data from a parent component to its child components. Props allow components to be dynamic and reusable, as they can receive different JSON data based on the parent's state or other factors. JSON arrays can also be passed as props, allowing for the transmission of multiple values in a single variable. JSON's simplicity and readability make it an ideal format for transmitting complex data structures between components in a React application.
Similar Reads
How to parse JSON Data into React Table Component ?
In React parsing JSON Data into React Table Component is a common task to represent and structure data. We can render the JSON data into a table dynamically using the array map.Prerequisites:NPM & Node JSReact JSJSON parse()ApproachTo render JSON data in React Table we will be using the JavaScri
2 min read
How to use Icon Component in React JS?
Every application requires icons for UI purposes, and integrating them with Material UI for React is seamless. Utilize the Icon Component in ReactJS effortlessly by following this straightforward approach. Prerequisites:NodeJS or NPMReact JSMaterial UISteps to create React Application And Installing
2 min read
How To Use Modal Component In ReactJS?
Modals are a popular UI component used to display content in a pop-up window like alerts, forms, or additional information. In ReactJS, there are multiple ways to implement a modal. In this article, we will discuss two common approaches: using reusable functional components and using the Material-UI
4 min read
How to use Chip Component in React JS?
Chips are compact elements that represent an input, attribute, or action. Material UI for React has this component available for us, and it is very easy to integrate. We can use the Chip Component in React JS using the following approach.Prerequisites:Node JS or NPMReact JSMaterial UISteps for Creat
2 min read
How to use List Component in ReactJS?
Lists are continuous, vertical indexes of text or images. Material UI for React has this component available for us, and it is very easy to integrate. We can use the List Component in ReactJS using the following approach. Prerequisites:NodeJS or NPMReactJSSteps to Create the React Application And In
2 min read
How to Map Data into Components using ReactJS?
Mapping data in react js component is a comman practice to render the lists and repeating elements. In this article, we will have a users data and we will map it to react components to display the users information in the react appPrerequisites:React JSNodejs and npmJavaScript Array mapApproachTo ma
3 min read
How to use Rating Component in ReactJS ?
Rating Component helps capture the user feedback in form of rating. With the help of this feature, users can also rate the products they have purchased. Material UI for React has this component available for us, and it is very easy to integrate. We can use the following approach in ReactJS to use th
2 min read
How to import components in React Native ?
React Native is a framework developed by Facebook for creating native-style applications for Android & iOS under one common language, i.e. JavaScript. Initially, Facebook only developed React Native to support iOS. However, with its recent support of the Android operating system, the library can
5 min read
How to Convert CSV to JSON in ReactJS ?
Dealing with data in various formats is a common task in web development. CSV (Comma Separated Values) and JSON (JavaScript Object Notation) are two widely used formats for storing and transmitting data. Converting CSV to JSON is often required when working with data in ReactJS applications. Approac
4 min read
How to Pass a React Component into Another to Transclude Content?
Transclusion refers to Passing a react component's content to another component. Passing component to another in react enable use to render the data inside other component. This content can be rendered as component's children.ApproachTo pass a react component to another in react we will pass it as t
2 min read