How to target Nested Objects from a JSON data file in React ? Last Updated : 05 Apr, 2024 Comments Improve Suggest changes Like Article Like Report Accessing nested JSON objects is similar to accessing nested arrays. Suppose we have a JSON object called person with a nested object 'address' , and we want to access the 'city' property from the address object. We can do this using dot notation like person.address.city. This notation allows us to directly access properties of nested objects within the JSON structure. Example: const person = { "name": "Geeks", "address": { "street": "Sector 136 Noida", "city": "Noida", "zipcode": "201310" }, "email": "geeksforgeeks.org"};const city = person.address.city;console.log("City:", city); // Outputs: City: NoidaThere are two main ways to target nested objects from a JSON data file: Table of Content Targeting Nested Objects with Dot NotationBracket Notation for Dynamic Keys and ArraysTargeting Nested Objects with Dot NotationTargeting nested objects from a JSON data file using dot notation involves specifying the path to the desired property by chaining object keys with dots. For example, accessing a property like 'person.address.city' targets the 'city' property within the nested 'address' object of the 'person' object. This approach simplifies navigation through nested structures and facilitates direct access to specific values within the JSON data. Example: This example shows the targeting objects using dot notation. JavaScript import React from 'react' const NestedObjectExample = () => { // Sample JSON data const jsonData = { person: { name: 'Geeks', address: { street: 'Sector 136 Noida', city: 'Noida', zipcode: '201310' }, email: 'geeksforgeeks.org' } } // Access nested object using dot notation const cityName = jsonData.person.address.city return ( <div> <h1>City Name:</h1> <p>{cityName}</p> </div> ) } export default NestedObjectExample Output: Bracket Notation for Dynamic Keys and ArraysBracket notation in JavaScript allows for dynamic access to object properties and array elements using variables or expressions within square brackets ( [ ] ). This is particularly useful when dealing with dynamic keys or indices in JSON data structures, enabling flexible and dynamic data manipulation in your code. Example : This is another example to showcase for dynamic keys and Arryas. JavaScript import React from 'react' const DynamicKeysAndArraysExample = () => { // Sample JSON data const jsonData = { person: { name: 'geeks', address: { street: 'Sector 136 Noida', city: 'Noida', zipcode: '201310' }, email: 'geeksforgeeks.org' }, fruits: ['apple', 'banana', 'orange'] } // Define dynamic keys and array index const key = 'person' const index = 1 // Access nested object with // dynamic key and array element const dynamicObject = jsonData[key] const dynamicProperty = dynamicObject.address.city const arrayElement = jsonData.fruits[index] return ( <div> <h2>Dynamic Property: {dynamicProperty}</h2> <h2>Array Element: {arrayElement}</h2> </div> ) } export default DynamicKeysAndArraysExample Output: Example 2: This example shows fetching json data from an API and then accessing data. JavaScript import React, { useState, useEffect } from 'react' const NestedObjectExample = () => { const [city, setCity] = useState('') useEffect(() => { // Fetch JSON data from the file fetch('https://jsonplaceholder.typicode.com/users') .then((response) => response.json()) .then((data) => { // Access nested object using dot notation const cityName = data[0].address.city setCity(cityName) }) .catch((error) => console.error('Error fetching data:', error)) }, []) return ( <div> <h1>City Name:</h1> <p>{city}</p> </div> ) } export default NestedObjectExample Output: Comment More infoAdvertise with us Next Article How to target Nested Objects from a JSON data file in React ? ranakrunal2704 Follow Improve Article Tags : Web Technologies ReactJS React-Questions Similar Reads How to fetch data from a local JSON file in React Native ? Fetching JSON (JavaScript Object Notation) data in React Native from Local (E.g. IOS/Android storage) is different from fetching JSON data from a server (using Fetch or Axios). It requires Storage permission for APP and a Library to provide Native filesystem access.Implementation: Now letâs start wi 3 min read How to Load Data from a File in Next.js? Loading Data from files consists of using client-side techniques to read and process files in a Next.js application. In this article, we will explore a practical demonstration of Load Data from a File in Next.js. We will load a validated CSV input file and display its contents in tabular format. App 3 min read How to load a JSON object from a file with ajax? Loading a JSON object from a file using AJAX involves leveraging XMLHttpRequest (XHR) or Fetch API to request data from a server-side file asynchronously. By specifying the file's URL and handling the response appropriately, developers can seamlessly integrate JSON data into their web applications. 3 min read How to access nested object in ReactJS ? To access a nested object in react we can use the dot notation. But if we want to access an object with multi-level nesting we have to use recursion to get the values out of it. PrerequisitesReact JS NPM & Node.jsJavaScript ObjectApproachThe structure of an object in React JS can be nested many 2 min read How to Filter Nested Object in Lodash? Lodash is a popular JavaScript utility library that simplifies common tasks like manipulating arrays, objects, strings, and more. In this tutorial, we will explore how to filter nested objects using Lodash's _.filter() and _.get() methods. This technique is useful when we need to filter data, such a 2 min read How to Access and Process Nested Objects, Arrays, or JSON? Working with nested objects, arrays, or JSON in JavaScript involves traversing through multiple levels of data. Here are some effective ways to access and process nested data1. Using Dot Notation and Bracket Notation â Most CommonDot notation is commonly used for direct access, while bracket notatio 3 min read How To Delete An Item From State Array in ReactJS? It is important to manage the state in ReactJS for building interactive user interfaces. Sometimes when we work with arrays, we need to remove an item from these arrays. A common use case is deleting an item from an array stored in the componentâs state. In this article, weâll explore different ways 3 min read How to Access Nested Fields in React-Bootstrap-table ? React-Bootstrap-Table provides a set of components and utilities that make it easy to create tables with features like sorting, filtering, pagination, and more. To access nested fields in React-Bootstrap-Table, you can use the dataField property of your column definitions to specify the path to the 2 min read How to access mapped objects in another function in ReactJS ? Following is the simple approach to access objects of array.map() to another function in React We can directly call the function in array.map() and pass the object in that function to return the required value. Setting up environment and Execution: Step 1: Create React App command npx create-react-a 2 min read How to set an object key inside a state object in React Hooks? We can update a React hooks state object that has a nested object containing objects with index keys with the following approach, Before doing so, consider the following example: Example: Following is the default state object: const [data, setData] = useState({ name:'', contact:'', address:{ 0:{}, 1 2 min read Like