Access nested keys using nodejs

accessing nested objects javascript
get value from nested json object in javascript
javascript find value in nested object
lodash get nested property
javascript get nested object property by string
javascript access nested object property with variable
javascript access nested array
lodash access nested object

I have a requirement that, I want to make a list of keys and return using nodejs.

Here is my json structure:

{
        type: "qwqw",
        org: "asdasd",
        pop: "asaswe",
        results: {
            "resultId1": {
                "type": "normal"
                .............
                ..............
             },
             "resultId2": {
                "type": "normal"
                .............
                ..............
             },
             "resultId3": {
                "type": "normal"
                .............
                ..............
             }
       }

So here, desired outcome is; something like this ["resultId1", "resultId2", "resultId3"]

This is what I tried :

 var keys = Object.keys(jsonString.results);
    for (var i = 0; i < keys.length; i++) {
        console.log(data[keys[i]]);
    }

Could someone help me with the json parsing using any nodejs library;

you should try this, as it is quite simple to get keys in javascript.

var data =  {
                type: "qwqw",
                org: "asdasd",
                pop: "asaswe",
                results: {
                    "resultId1": {
                        "type": "normal"
                     },
                     "resultId2": {
                        "type": "normal"
                     },
                     "resultId3": {
                        "type": "normal"
                     }
                }
           };
           
var output = Object.keys(data.results);
console.log(output);

nested-keys, Description. Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library can do this!) Access Nested Objects Using Array Reduce Array reduce method is very powerful and it can be used to safely access nested objects. const getNestedObject = (nestedObj, pathArr) => { return pathArr.reduce((obj, key) => (obj && obj[key] !== 'undefined') ? obj[key] : undefined, nestedObj); }

data.json file: { "type": "qwqw", "org": "asdasd", "pop": "asaswe", "results": { "resultId1": { "id": "4001", "type": "normal" }, "resultId2": { "id": "5001", "type": "Normal" }, "resultId3": { "id": "6001", "type": "Normal" } } }

Node.js Program :
//Program by Atharva Jawalkar
var fs = require("fs");
var dir = __dirname + "/data.json";
fs.readFile(dir, "utf8", function(err, data) {
  if (err) throw err;
  var obj = JSON.parse(data);

  var obj2 = obj.results;

  var keysArray = Object.keys(obj2);
  var len = keysArray.length;
  for (var i = 0; i < len; i++) {
    var key = keysArray[i]; 
    var value = obj2[key]; 
    console.log(key, value);
  }
});

Output:
resultId1
json_parsing_try3.js:15
Object {id: "4001", type: "normal"}
json_parsing_try3.js:15
resultId2
json_parsing_try3.js:15
Object {id: "5001", type: "Normal"}
json_parsing_try3.js:15
resultId3
json_parsing_try3.js:15
Object {id: "6001", type: "Normal"}

Here i have created data.json file and read that file in program but you can declare your json data in variable also.

Accessing Nested Objects in JavaScript, tldr; safely access nested objects in JavaScript in a super cool way. we'll be dealing with nested objects and often we'll be needing to access the innermost nested values safely. It is available as an npm package — Typy. This is a short post intended to show the many different ways on how to safely access deeply nested values in JavaScript. The following examples all do the same thing and while they may vary in…

You have an error in your code:

var keys = Object.keys(jsonString.results);
for (var i = 0; i < keys.length; i++) {
    console.log(data[keys[i]]);
}

should be

var keys = Object.keys(jsonString.results);
for (var i = 0; i < keys.length; i++) {
    console.log(keys[i]);
}

Notice the difference on line 3, in the console.log parameter.

Safely Accessing Deeply Nested Values In JavaScript, This is a short post intended to show the many different ways on how to safely access deeply nested values in JavaScript. The following  An easy way of accessing the nested elements can be found on the answer of following thread (Accessing nested data structures). A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.

How to access nested json objects in JavaScript?, Accessing nested json objects is just like accessing nested arrays. Nested objects are the objects that are inside an another object.In the  2. Access Nested Objects Using Array Reduce. To be able to access nested arrays, you can write your own array reduce util.

How do I build a nested object in Javascript dynamically , Hi FCC, I've been struggling with this problem for a day or two and I have been able to get close but not the full solution that I am looking for. Say I have two The values in arr2 are not related to arr1 at all. Below would be a  Node.js is a JavaScript runtime primarily used for creating web applications. Put another way, it's a server-side implementation of JavaScript used for writing the backend of an application. (Though many Node.js frameworks can also handle the frontend.) Here are a few examples of what you might create with Node.js.

Accessing Nested Objects - JavaScript, Hello! So for the Javascript challenges, I am stuck in the “Accessing Nested Objects” part. The sub-properties of objects can be accessed by chaining together  Object.keys () returns an array whose elements are strings corresponding to the enumerable properties found directly upon object. The ordering of the properties is the same as that given by looping over the properties of the object manually. // simple array const arr = ['a', 'b', 'c']; console.log (Object.keys (arr)); // console: ['0', '1', '2

Comments
  • Before voting down, please comment it; so that I can correct it.
  • What have you tried? What is your desired output?
  • Updated the question; please check
  • You've already solved your problem. var keys = Object.keys(jsonString.results);
  • Could you include your whole code in your question ? (Including the way to import your data) Edit : By the way, your for loop is completly useless. just print the keys variable.