gpt4 book ai didi

javascript - 如何评估 JSON 的键以匹配特定键并提取该键的值?

转载 作者:行者123 更新时间:2023-11-30 09:18:12 24 4
gpt4 key购买 nike

我有一个非常复杂的 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"
}

最佳答案

你需要一个递归搜索功能。

以下 ES6 代码段递归遍历 obj 中的所有 Node ,直到找到 key

它不假定包含数组的 Node 用“子段”标识。但是您可以执行 k == 'subsegments' && Array.isArray(obj[k]) 来执行此规则。

search = (obj, key) => {
let res = null;

(searchNode = obj => Object.keys(obj).some(k => {
if(k == key) {
res = obj[k];
return true;
}
return Array.isArray(obj[k]) && obj[k].some(searchNode);
}))(obj);

return res;
}

obj = {
"id": "*******",
"name": "createNewApiKey",
"start_time": 1543504061.474,
"end_time": 1543504062.059,
"parent_id": "******",
"subsegments": [
{
"subsegments": [
{
"subsegments": [
{
"subsegments": [
{
"metadata": {
"default": {
"inputData": {
"id": "*****",
"givenClientName": "abc1012",
"productOwner": "dev"
},
"response": "** successfully"
}
}
}
]
}
]
}
]
}
]
};

console.log(search(obj, 'metadata'))

关于javascript - 如何评估 JSON 的键以匹配特定键并提取该键的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53541830/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com