0

I've returned to the Express stack after a lot of work in Google Go for the last few months. I'm building simple webapp page for work, iterating JSON objects. I'm trying to print the value of each key in an object that is part of the larger object, but only the key gets logged?

Here is my data.json that I read over (showing only 1 object in the entire object and omitting data for security.):

{
    "Prod18r2" : {
        "baseAPIURL" : "https://apiurl.com/",
        "serviceEndpoints" : {
            "restaurantManager" : "https://prodapiurl.com/rm/",
            "transactionManager" : "https://prodapiurl.com/rm/",
            "userManager" : "https://prodapiurl.com/rm/",
            "recordHistory" : "https://prodapiurl.com/rm/"
        }
    }
}

Here is my iterate-and-log logic right now in a test.js file:

for(var x in INSTANCES.Prod18r2.serviceEndpoints) {
    console.log(x);
}

Output:

PS C:\Users\payton.juneau\Desktop\Me\Projects\Node\etm-scry-webapp>node .\test.js restaurantManager transactionManager userManager recordHistory PS C:\Users\payton.juneau\Desktop\Me\Projects\Node\etm-scry-webapp>

Thanks in advance for any reply, I know this is probably embarrassingly dumb.

  • 1
    The value is `INSTANCES.Prod18r2.serviceEndpoints[x]`. – Barmar Mar 29 '18 at 16:56
  • You might wanna have a look at `Object.entries` which gives you key-value pairs. – Jonas Wilms Mar 29 '18 at 16:57
  • @Barmar Wow, you've done it! This is so confusing to me though, because it is not array in the JSON. It is nested object. Also, I thought my logic made sense, being for every.. oh.. for every KEY in the nested object print the key.. I realized half way through writing.. Anyway, thanks!! –  Mar 29 '18 at 16:58
  • @JonasW. Hey, I saw this while searching around but dismissed it because I didn't think it was exactly the solution I needed, although I should probably read it to see if it is of any help. –  Mar 29 '18 at 16:59
  • Yup. `for(const [key, value] of Object.entries(someObj))` is quite useful... – Jonas Wilms Mar 29 '18 at 17:02
  • @KeplerIO `[]` is used for both array indexes and object keys. – Barmar Mar 29 '18 at 17:05

2 Answers2

1

It need object[keyName] to get the values

var INSTANCES = {
  "Prod18r2": {
    "baseAPIURL": "https://apiurl.com/",
    "serviceEndpoints": {
      "restaurantManager": "https://prodapiurl.com/rm/",
      "transactionManager": "https://prodapiurl.com/rm/",
      "userManager": "https://prodapiurl.com/rm/",
      "recordHistory": "https://prodapiurl.com/rm/"
    }
  }
}
//Here is my iterate-and-log logic right now in a test.js file:

for (var x in INSTANCES.Prod18r2.serviceEndpoints) {
  console.log(INSTANCES.Prod18r2.serviceEndpoints[x]);
}
brk
  • 48,835
  • 10
  • 56
  • 78
0

You should make

for(var serviceEndpoints in INSTANCES.Prod18r2.serviceEndpoints) {
    console.log(INSTANCES.Prod18r2.serviceEndpoints[serviceEndpoints]);
}

I replace your "x" for "serviceEndpoints" and take the json value using the key returned.

Miguel Angel
  • 944
  • 8
  • 20