2

I'm facing issues in retrieving the JSON data.

Here am getting the JSON with dynamic keys. And I need to access that dynamic key's values. The dynamic key is coming from URL and am fetching that key's data from DB.

Here is my sample data.

let roleData = [
  {
    "assetCategory": {
       "canCreate": false,
       "canView": false,
       "canUpdate": false,
       "canDelete": false,
       "isMenu": false,
       "parent": "settings"
     }
  }
]

In the above JSON, I've got assetCategory object. But this value is completely dynamic. I might get other values too in the place of assetCategory. So without knowing this key, it's getting difficult for me to access the data.

Is there any way to access this dynamic object?

3 Answers 3

4

let data = [
  {
    "assetCategory": {
       "canCreate": false,
       "canView": false,
       "canUpdate": false,
       "canDelete": false,
       "isMenu": false,
       "parent": "settings"
     }
  }
]


let unknownNames = data.map( item => Object.keys(item)) // returns ['assetCategory']
//Returns array of all the unknown names
console.log(unknownNames);

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks. But when I use the above solution, am getting this as the result in console [ [ '$__', 'isNew', 'errors', '_doc', '$locals', '$op', '$init' ] ]
This seems to work. repl.it/repls/IcyBubblyStructures Maybe try using a different variable name besides 'data'
It's working in onine editors. But not in my code. Still getting, $__
This is what am giving as the input to the code you have commented
[ { assetCategory: { canCreate: false, canView: false, canUpdate: false, canDelete: false, isMenu: false, parent: 'settings' } } ]
|
4

You should use Object.keys(...) : https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

For example:

const a = [
  {
    "assetCategory": {
       "canCreate": false,
       "canView": false,
       "canUpdate": false,
       "canDelete": false,
       "isMenu": false,
       "parent": "settings"
     }
  }
];

const name = Object.keys(a[0])[0]; // --> "assetCategory"
console.log(a[0][name]) // --> {"canCreate": false, ...}

1 Comment

When I use your answer, am getting this, $__. What does this mean?
1

you can get all keys by Object.keys(data[0].assetCategory). and then data[0].assetCategory[varWithKeyValue] or if it's dynamic data[0].assetCategory['can' + 'Create']

3 Comments

No, I want to get assetCategory object itself. But the problem is, assetCategory is a dynamic key. In that place I might get assetStatus and I should get assetStatus 's object too
then you can do the same with Object.keys(data[0]).
in case if you want to get only keys of the object the modern way is Object.getOwnPropertyNames(data[0]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.