Converting JSON data from a MySQL database into an array of objects

I'm having trouble retrieving an object array from my MySQL database. Every time I run my code, I keep getting an "undefined" result and I can't figure out why.

database.query(sql, (err, rows)=>{
    var data = Object.values(JSON.parse(JSON.stringify(rows)))
    console.log(data[0].type)
})

When I console log the "data" variable, I see:

[
  {
    data: '[{"type": "c#", "script": "csharp script"}, {"type": "javascript", "script": "javascript script"}, {"type": "html", "script": "html script"}]'
  }
]

This output seems correct to me, but I'm not sure how to proceed from here. Any assistance would be greatly appreciated

Answer №1

The necessary information can be found within the initial element's .data attribute. This data is in string format, so it must be converted into an array of objects for use.

Sample implementation

let storedInfo = [{
  data: '[{"category": "python", "script": "python script"}, {"category": "java", "script": "java script"}, {"category": "css", "script": "css script"}]'
}]

let parsedData = JSON.parse(storedInfo[0].data)
console.log(parsedData[0].category)

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

Using jQuery .css({}) is not causing negative margin to function as expected

$('#thankYouMessage').css({"height": textHeight, "margin-top:": "-52px", "padding-left": "19px"}); The CSS property 'padding-left:' will be applied as expected, but the negative margin will not take effect. The 'margin-top:' ...

Gathering all the indicated whole numbers from a string and placing them in an array

Upon decoding the URL query using decodeURIComponent and splitting it, the resulting array looks like this: ["pcs_availability:Online", "price:[1500 TO 1999.99]"]. I am trying to extract the proper integers from this array as shown in [1999.99]. In some ca ...

Creating a scale effect similar to iCloud.com in Angular.JS: A step-by-step guide

Have you checked out the new icloud.com site? There's a cool effect on there that I want to try and recreate for a project I'm working on. When you go to and log in, you'll notice a loading gif followed by the calendar app scaling out to t ...

Creating a 2D array of pointers in ANSI C through dynamic allocation

I have come across a scenario where I need to create an array of pointers (M rows, N cols), with each pointer pointing to a float vector of variable length (L). Can you guide me on how to dynamically establish this array? The challenge is that the values o ...

How can you use CakePHP3 to incorporate user notifications in a style similar to Facebook or a stack format through view cells?

When using platforms like Facebook or StackOverflow, we often receive notifications on the top navigation bar without refreshing the webpage. These are known as push notifications. I have a functional CakePHP 3 web app and my client wants to incorporate t ...

What could be causing my form not to Submit when attempting submission without utilizing the submit button?

Here is the code for my validation: $(function () { function validateForm() { // Code to validate form inputs } $('#myform').submit(validateForm); }); After this, I want to submit the form when a certain action occurs: < ...

The Highchart is failing to display a single data point

The marker I set is not displaying when there is only a single data point available. The data point is only showing up when we hover on the chart. Check out an example fiddle here - jsfiddle ...

What are the steps to access data from a JSON file with Jquery and ajax?

Today, something strange happened to me: I was attempting to access data from a JSON file using jquery and ajax, then showcase this information on a webpage. The example I followed from the web worked perfectly on my main OS. However, when I tried to run i ...

Tips on validating emails using mongoose

As a beginner with Mongoose, I have a code snippet here. How can I validate the text that appears after the @ symbol in an email before saving it to the database? var user = new User ({ firstName: String, lastName: String, email: String, ...

Accessing state in a Vuex store module

Looking for guidance on how to access module store/state from another file. Here is a snippet of my code: /store/index.js import Vuex from 'vuex'; import categories from './modules/categories'; Vue.use(Vuex); const store = new Vuex.S ...

Is it feasible to implement different themes besides 'light' and 'dark' in React Material UI?

Currently, I am developing a user interface (UI) using React in combination with Material UI (v5+). The documentation provides information on how to utilize the extendTheme function along with CssVarsProvider to incorporate MUI colors into CSS. It also men ...

Troubleshooting problem with connecting AngularJS ng-click within a directive and ControllerAs

ShowPopover is not triggering when the directive is clicked. Can you please assist me in diagnosing the source of this issue? Directive: angular.module('landingBuilder').directive('popoverDirective', popoverDirective); functi ...

React component encounters rendering issue with if-else statement

I'm attempting to use an if-else statement to check if the photo property contains an item, but for some reason, the code below is not working as expected. I can't seem to figure out what's wrong with it. Everything appears to be correct in ...

Creating a sequential series of numbers for a website form using PHP

As a novice in website development for my project, I am looking to create an ascending series number that will serve as the primary key for my database table. This will make it easier for users to input data and for me to quickly identify their data based ...

Increasing time in MySQL by hours

Can you help me figure out how much time a user has left to respond to a specific order? In our company, we operate on shift timings from 9:00 AM to 4:00 PM, giving users 4 office hours to work. For example, if a client places an order at 3:00 PM, the use ...

The JavaScript Ajax data is in array format, however, the length of the array

I received the following Json data: (Extracted from the console) jsonData Object {0: Object, 1: Object, 2: Object, 3: Object, 4: Object, 5: Object, 7: Object} When I enter jsonData[0] in the console, this is the output: Object {id: 125, Module_id: 2, a ...

Building a sub route in React Router v4 allows developers to efficiently organize and manage

Before starting, I familiarized myself with the concept of react router by visiting https://reacttraining.com/react-router/web/guides/quick-start. I have developed a simple 3-page site in react and now want to create a list that will allow me to display so ...

Combining edit and view functionalities in jqGrid form

I've been exploring the jqGrid wiki but I'm having trouble finding answers to a couple of things. It seems like it might be too customized. 1) Can jqGrid be configured to display all fields of a row when editing via form edit, but only make a fe ...

Sending parameters with POST request in Android Volley to retrieve JSON data

I'm currently working on a Volley POST request that includes the parameter friends_phone_number_csv and is expected to return a JSON object. However, the request I have been using keeps showing the message: E/Volley﹕ [4230] BasicNetwork.performReq ...

Steps to prepend a domain to file_get_contents function in PHP script

Recently, I enlisted the help of a PHP coder to make modifications to two files for me. The purpose was to allow users to link directly to a file converted from DOC to IMG on . Below you can find the edited code (highlighted by 'DrTech76'): get ...