Get names of keys in objectpath

95 Views Asked by At

How would I get the names of the keys, for example [800, 801] (the key names are unknown) with objectpath. It is easy in jmespath: keys(@).

  "groups": {
    "800": {
      "short_name": "22",
      "oname": "11",
      "group": 8,
      "title": "SS",
      "name": "33",
      "onames": [""],
      "alt_name": False,
      "waytype": 1,
      "multiple": 1,
      "primary": 1
    },
    "801": {
      "short_name": "ss",
      "oname": "zz",
      "group": 8,
      "title": "ss",
      "name": "bbb",
      "onames": [""],
      "alt_name": False,
      "waytype": 1,
      "multiple": 1,
      "primary": 0
    },
1

There are 1 best solutions below

0
On

let your object is assigned to name variable

const name = {    "groups": {

    "800": {
      "short_name": "22",
      "oname": "11",
      "group": 8,
      "title": "SS",
      "name": "33",
      "onames": [""],
      "alt_name": false,
      "waytype": 1,
      "multiple": 1,
      "primary": 1
    },
    "801": {
      "short_name": "ss",
      "oname": "zz",
      "group": 8,
      "title": "ss",
      "name": "bbb",
      "onames": [""],
      "alt_name": false,
      "waytype": 1,
      "multiple": 1,
      "primary": 0
    }   } }

Use for loop to get the key name as

 for(var num in name.groups) {   
        console.log(num);
    }

and to get the values of key

for(var num in name.groups) {
  console.log(name.groups[num]);
}