How to acces to specific properties into a JSON or Object in JavaScript

how to get key and value from json array object in javascript
get value from json object in javascript
get value from nested json object in javascript
get key value from json object in java
create json object in javascript dynamically
json.parse javascript
json.parse array of objects
loop through json object

I'm making an app in Nodejs using express and node-xlsx module, what I want to do is to make the user able to upload an xlsx file (which has to have an specific format of two columns), an then the server reads it and does something with each row of the file.

(Example of my test file, being the columns A and B respectively):

    Johny Wilson    | jonhny@email.com
    Andrew Jehnsen  | andrew@example.com
    Billy Soon      | billy@mail.com

In order to do this, I've decided to use the node-xlsx module, which, after reading the file with this code:

    var xlsx = require('node-xlsx');

    router.post('/enviar', upload.single("lista"),(req, res, next) =>{
        //dir is the path of the xlsx file
        const workSheetsFromFile = xlsx.parse(dir); 
        res.send(workSheetsFromFile);
    });

returns an object that looks like this:

    [
        {
            "name": "Hoja1",
            "data": [
                [
                    "Johny Wilson",
                    "jonhny@email.com"
                ],
                [
                    "Andrew Jehnsen",
                    "andrew@example.com"
                ],
                [
                    "Billy Soon",
                    "billy@mail.com"
                ]
            ]
        }
    ]

As you can see, the module returns the data of all the file, including the sheet's details (In this case only one), I want to access only to the 'data' array which contains keys and values to process them.

I've already tried to loop on the data array with:

    workSheetsFromFile.data.forEach((element) =>{
        console.log(element);
    });

and

    workSheetsFromFile[data].forEach((element) =>{
        console.log(element);
    });

and

    workSheetsFromFile['data'].forEach((element) =>{
        console.log(element);
    });

but all of them just send me an error like "Cannot read property 'forEach' of undefined" or "data is not defined" :(

For now, with those few lines of code I was specting to iterate the data array and print each pair of key and value, so once that is fixed, inside this loop process each key and value in order to send automated mails.

What you have here seems to be an array of objects, not an object itself!

try

 workSheetsFromFile[0].data.forEach((element) => {
     console.log(element);
 });

If you have more elements, consider first looping the array and then extracting data

JSON Objects, JSON objects are written in key/value pairs. Keys must be strings, and Try it Yourself ». In a for-in loop, use the bracket notation to access the property values:​  To access the JSON object in JavaScript, parse it with JSON.parse (), and access it via “.” or “ []”.

const structuredData = workSheetsFromFile[0].data.map(res => {
     return res
});

How to access JSON object in JavaScript, now how can u get property inside it. im working on a javascript file and i get error message Unexpected number. Vote Up0  JSON objects are surrounded by curly braces {}. JSON objects are written in key/value pairs. Keys must be strings, and values must be a valid JSON data type (string, number, object, array, boolean or null). Keys and values are separated by a colon. Each key/value pair is separated by a comma.

workSheetsFromFile.forEach(sheet => {
   //access data
   console.log(sheet.data)

   //or

   sheet.data.forEach(data => {
   //access each data
   console.log(data)
   })
})

Access and print a specific JSON value, JSON wouldn't be very useful if you want from the JSON object. Sponsored message: At Stoplight, everything starts print it to the JavaScript Console,  This sample gets an object's T:Newtonsoft.Json.Linq.JProperty collection using M:Newtonsoft.Json.Linq.JObject.Properties.

JSON methods, toJSON, JSON.parse to convert JSON back into an object. specification, so some JavaScript-specific object properties are skipped by JSON.stringify . Accessing JavaScript Properties. The syntax for accessing the property of an object is: objectName.property // person.age. or. objectName [ "property" ] // person ["age"] or. objectName [ expression ] // x = "age"; person [x] The expression must evaluate to a property name.

How To Work with JSON in JavaScript, Converting JSON objects into strings can be particularly useful for If we want to access the data in the JavaScript object above, we could use  Getting a specific property from a JSON response object. JSON wouldn’t be very useful if you always had to print out the entire response. Instead, you select the exact property you want and pull that out through dot notation. The dot (.) after response (the name of the JSON payload, as defined arbitrarily in the jQuery AJAX function) is how you access the values you want from the JSON object.

Working with JSON, In the next article, we'll begin looking at object-oriented JavaScript. JavaScript, including parsing JSON so you can access data within it, and creating JSON. Inside that we want to access the members property, so we use  If we want to access the data in the JavaScript object above, we could use dot notation to call user.first_name; and get a string, but if we want to access the full name, we would need to do so by calling user.full_name(); because it is a function.

Comments
  • Possible duplicate of Access / process (nested) objects, arrays or JSON
  • yeah sure.Not good code practice, but it works perfectly too.