My JSON data structure appears as follows:
{
'total_count': 6,
'incomplete_results': false,
'items': [
{
'url': 'https://api.github.com/repos/Samhot/GenIHM/issues/2',
'repository_url': 'https://api.github.com/repos/Samhot/GenIHM',
'comments_url': 'https://api.github.com/repos/Samhot/GenIHM/issues/2/comments',
'events_url': 'https://api.github.com/repos/Samhot/GenIHM/issues/2/events',
'html_url': 'https://github.com/Samhot/GenIHM/issues/2',
'id': 293234257,
'number': 2,
'title': 'Create server for RESTful API',
'user': {
'login': 'Samhot',
'id': 7148311,
'avatar_url': 'https://avatars3.githubusercontent.com/u/7148311?v=4',
'gravatar_id': '',
'url': 'https://api.github.com/users/Samhot',
'html_url': 'https://github.com/Samhot',
'followers_url': 'https://api.github.com/users/Samhot/followers',
'subscriptions_url': 'https://api.github.com/users/Samhot/subscriptions',
'organizations_url': 'https://api.github.com/users/Samhot/orgs',
'repos_url': 'https://api.github.com/users/Samhot/repos',
'received_events_url': 'https://api.github.com/users/Samhot/received_events',
'type': 'User',
'site_admin': false
},
'state': 'open',
'locked': false,
'assignee': null,
}
]
};
I am attempting to extract all the keys from this JSON using the getDeepKeys()
function :
getDeepKeys2(obj) {
const keys = Object.keys(obj);
const childKeys = keys
.map(key => obj[key])
.map(
value =>
Array.isArray(value)
? this.getDeepKeys2(value[0]) : typeof value === 'object'
? this.getDeepKeys2(value) : []
)
.reduce((acc, keys) => [...acc, ...keys], []);
this.dispotest = [...keys, ...childKeys];
return this.dispotest;
}
This method functions flawlessly unless the JSON includes a null or undefined value like 'assignee': null
.
If such a value is present in my JSON, the function will result in:
ERROR TypeError: Cannot convert undefined or null to object
I am considering utilizing the typeof
function by implementing
if (typeof value === null { return null; }
but I am unsure about where to incorporate this...
Any assistance will be greatly appreciated! Thank you!