Error thrown due to inability to map imported JSON data

Displayed below is a snippet of data stored in the data.json file:

[
  {"id": 23, "name": "Good!", "state": "OK"},
  {"id": 24, "name": "Not good...", "state": "Fail"},
  {"id": 26, "name": "Oh...", "state": "OK"},
  {"id": 27, "name": "What?", "state": "Fail"}
]

Utilizing a script below, an attempt is made to map the data:

import * as data from './data.json'

let jsonData = data
console.log(jsonData)

jsonData = jsonData.map(({name, state}) => ({name, state}))
console.log(jsonData)

Displayed below is the output:

{default: Array(4)}
default
:
(4) [{...}, {...}, {...}, {...}]
0
:
(3) {id: 23, name: "Good!", state: "OK"}
1
:
(3) {id: 24, name: "Not good...", state:...}
2
:
(3) {id: 26, name: "Oh...", state: "OK"}
3
:
(3) {id: 27, name: "What?", state: "Fail...}
TypeError: jsonData.map is not a function
    at <anonymous>:35:21
    at dn (<anonymous>:16:5449)

I am perplexed as the first console.log() call appears to output something iterable. Why then does it result in a TypeError?

I have attempted various solutions like:

let jsonData = JSON.parse(JSON.stringify(data))
but none have proven to be successful.

What steps should be taken to parse the data from the file in order to be compatible with the map() method?

Answer №1

Hooray! You have successfully stumbled upon the mysterious realm of JavaScript modules.

Behold, here lies the key to your desires:

import info from './info.json'

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Preventing Component Duplication in Vue.js: Tips to Avoid Displaying Two Instances on the Screen

After developing a Vue app in VS Code, I encountered an issue where the home component was rendered twice on the screen when attempting to run the application. Below is a screenshot of the resulting homepage: Here is the code from Home.vue: ...

What is the best way to layer three or more canvases on top of each other?

I am trying to overlay three or more canvases correctly, but I am struggling. I have come across similar topics (How to place two canvases on top of one another?), but I can't seem to figure out how to stack all 3 or 4 canvases together. Whenever I ad ...

"Discover the process of retrieving a specific element from a JSON array within PL/SQL Oracle 12c

Querying a column in an Oracle 12c table that contains CLOB data, specifically in JSON format under the column name 'dynamic_fields'. The content of the column is structured as follows: { "App": 20187.7", "CTList": "[ {\"line ...

JavaScript encountered a ReferenceError: 'a' has not been declared

One interesting feature of my slider is that it uses links like <a id="foo" class="oslide">Chineese Food</a> to navigate through the slides. To make this navigation function properly, I had to include a.href = "#"; in the link's click even ...

Issue with marker functionality on Google Maps JavaScript API when conditions are not functioning correctly

I am currently working on plotting different markers on Google Maps by extracting data from a CSV file. I have incorporated the parsecsv-0.4.3-beta library to read the CSV file, and everything is functioning smoothly except for when I compare two fields to ...

I recently implemented a delete function in my code that successfully removes a row, but now I am looking to also delete the corresponding data from localStorage in JavaScript

I have successfully implemented a delete function in JavaScript that deletes rows, but I also want to remove the data from local storage. This way, when a user reloads the page, the deleted row will not appear. The goal is to delete the data from local s ...

Monitor the latest website address being typed into the browser

Is it possible to track the new URL entered by a user in the browser when they leave the current page using the onunload event? For example, if a user is currently on www.xyz.com/page1.aspx and then types a new URL into the browser, I want to capture that ...

Issue: The jQuery function with the ID jQuery1830649454693285679_1359620502896 did not receive a JSON

After going through numerous Q&As on the same problem, I couldn't find a solution specific to my issue. The situation involves a PHP script that outputs a JSON string: header('Content-Type: application/json'); echo $result; Here is th ...

Using Rxjs to dynamically map values from an array with forkJoin

Greetings! I have a collection of Boolean observables and would like to apply a logical AND operation. Currently, I am passing static values 'a' and 'b', but I am unsure of the number of elements in the totalKeys array. import { forkJoi ...

Refresh a div element automatically with information from a different webpage using jQuery

I've been attempting to automatically reload a page and fetch new data every 10 seconds, or even less. However, the codes I've tried so far have not been successful. Here is my current approach... // script // <script> $(document).rea ...

Text alignment issues cause animation to vanish

Utilizing particles.js, I set up a full-screen particle effect by specifying the animation to be full-screen with height: 100vh;. This worked perfectly as intended. However, when attempting to add text on top of the particle animation and center it vertica ...

Outputting HTML with functions

I have a function that returns HTML code. renderSuggestion(suggestion) { const query = this.query; if (suggestion.name === "hotels") { const image = suggestion.item; return this.$createElement('div', image.title); } ...

Building Multiplatform applications with Ktor offers the flexibility to easily serialize and deserialize JSON data with a List as

Is there a way to effectively utilize kotlin.serialize in conjunction with Ktor's HttpClient for JSON serialization and deserialization when dealing with lists as the root element? When setting up my HttpClient, I currently have: HttpClient { ...

Why does this code snippet throw an error if let is not hoisted or in the temporal dead zone? It could have simply used the global reference instead

var b = 6; { console.log(b) let b = 55 } When running this code snippet, I encounter the following error message: ReferenceError: Cannot access 'b' before initialization Why is the console.log(b) not displaying 6 as expected? ...

Help setting up Angular ng-class is needed

Hey there, I'm currently attempting to change the background color of my CSS based on the value of ng-class (true or false). Can someone help me out with this? <div id="home"> Summoner <div id="in ...

Guide on transferring Context to the loader in React-Router-6

In my development setup, I am utilizing Context for managing the global loading state and React-router-6 for routing. My approach involves incorporating loader functionality in order to handle API requests for page loading. However, a challenge arises when ...

Utilizing Ajax to fetch a div element from a web page

Hey there! I have a link set up that loads a page into a specific div ID, which is #ey_4col3. The issue I'm facing is that it loads the entire page along with all its contents, but what I really want to load from that page is just the content within ...

Executing NPM commands in a sequential manner

If I have the following npm scripts: "scripts": { "pre-build": "echo \"Welcome\" && exit 1", "build_logic": "start cmd.exe @cmd /k \"yo esri-appbuilder-js:widget && exit 1\"", "post_build": "start C:\ ...

Issue with converting string to Date object using Safari browser

I need to generate a JavaScript date object from a specific string format. String format: yyyy,mm,dd Here is my code snippet: var oDate = new Date('2013,10,07'); console.log(oDate); While Chrome, IE, and FF display the correct date, Safari s ...

Having trouble with jQuery show/hide not functioning properly?

My website has a sub-menu section with a shopping cart icon. When a user hovers over the icon, it reveals a hidden cart section. The user can then move the mouse into the cart section to remove items. The problem I'm facing is that if a user displays ...