我有一个非常复杂的JSON结构,其中嵌套了JSON和数组。我想从中获取一个包含关键元数据的对象(json)。JSON的顺序不是每次都固定的。我如何为那个JSON获取值?这是JSON结构示例
{
"id": "*******",
"name": "createNewApiKey",
"start_time": 1543504061.474,
"end_time": 1543504062.059,
"parent_id": "******",
"aws": {
"function_arn": "********",
"resource_names": [
"****"
],
"account_id": "******"
},
"trace_id": "1-5c0000bc-*****",
"origin": "AWS::Lambda::Function",
"subsegments": [
{
"id": "5d680f995ca8cfd9",
"name": "*******",
"start_time": 1543504061.503,
"end_time": 1543504061.977,
"fault": true,
"error": true,
"cause": {
"exceptions": [
{
"stack": [
{
"path": "/var/task/node_modules/aws-xray-sdk-core/lib/patchers/aws_p.js",
"line": 77,
"label": "captureAWSRequest [as customRequestHandler]"
},
{
"path": "/var/runtime/node_modules/aws-sdk/lib/service.js",
"line": 267,
"label": "addAllRequestListeners"
},
{
"path": "/var/runtime/node_modules/aws-sdk/lib/service.js",
"line": 191,
"label": "makeRequest"
},
{
"path": "/var/runtime/node_modules/aws-sdk/lib/service.js",
"line": 499,
"label": "svc.anonymous function [as getSecretValue]"
},
{
"path": "/var/task/index.js",
"line": 34,
"label": "exports.handler"
}
],
"message": "*****",
"type": "ResourceNotFoundException",
"remote": true
}
],
"working_directory": "/var/task"
},
"http": {
"response": {
"status": 400
}
},
"aws": {
"operation": "GetSecretValue",
"region": "eu-west-1",
"request_id": "******",
"retries": 0
},
"namespace": "aws",
"subsegments": [
{
"id": "*****",
"name": "Metadata",
"start_time": 1543504061.981,
"end_time": 1543504062.017,
"metadata": {
"default": {
"inputData": {
"clientName": "a",
"productOwner": "dev"
},
"response": "Wrong client ID"
}
}
}
]
},
{
"id": "********",
"name": "Initialization",
"start_time": 1543504060.726,
"end_time": 1543504061.47,
"aws": {
"*****"
}
},
{
"id": "********",
"name": "annotations",
"start_time": 1543504061.477,
"end_time": 1543504061.478,
"annotations": {
"User": "dev",
"Name": "a"
}
}
]
}
**可以包含JSON或数组
这里我想在JSON下面取
{
"inputData": {
"id": "*****",
"givenClientName": "abc1012",
"productOwner": "dev"
},
"response": "** successfully"
}
3条答案
按热度按时间nuypyhwy1#
你需要一个递归搜索函数。
下面的ES6代码段递归遍历***obj***中的所有节点,直到找到***key***。
它不假设包含数组的节点用“子段”标识。但是您可以执行
k == 'subsegments' && Array.isArray(obj[k])
来强制执行此规则。pbossiut2#
如果你只想找数组的第一个元素,那么你可以试试。
u1ehiz5o3#
这里有一种可能性,使用给定的键搜索深度优先的项目,如果没有找到,则返回
null
:在这里,您还可以编写
const getMeta = findFirstKey('metadata')
,然后简单地将该函数应用于您的数据。但请注意,如果您的数据太不一致,任何像这样将其规范化的尝试最终都会出现问题。如果你能在前面驯服它,你的代码的其余部分可能会更简单。