如何提取 json 对象数组中的深层和浅层嵌套字段

How to extract both deeply and shallowly nested fields in array of json objects

假设我有这个 json 对象:

{
    "lotOfJson": [
    {
        "value": "someName",
        "property": "name",
        "children": [],
        "constraints": {
            "IsValidName": "name someName isn't valid"
        }
    },
    {
        "value": [
            {
                "id": "firstBadId"
            },
            {
                "id": "secondBadId"
            }
        ],
        "property": "listOfIds",
        "children": [
            {
                "value": {
                    "id": "firstBadId"
                },
                "property": "0",
                "children": [
                    {
                        "value": "firstBadId",
                        "property": "id",
                        "children": [],
                        "constraints": {
                            "badIdError": "This Id is bad!"
                        }
                    }
                ]
            },
            {
                "value": {
                    "id": "secondBadId"
                },
                "property": "1",
                "children": [
                    {
                        "value": "secondBadId",
                        "property": "id",
                        "children": [],
                        "constraints": {
                            "badIdError": "This Id is bad"
                        }
                    }
                ]
            }
        ]
    }
]

}

这个数组可以有很深的嵌套JSON——无法知道有多深。

每当有一个块看起来像:

"value": "",
"property": "",
"children": [],
"constraints": {
    "": ""
}

我想提取 valuepropertyconstraints 的值并将它们保存在一个数组中。对于我上面的 "lotsOfJson" 示例,这看起来像: ["someName", "name", "name SomeName isn't valid", "firstBadId, "id", "This Id is bad!", "secondBadId, "id", "This Id is bad!"]

所以我只提取值、属性 和约束,当它们像上面的块一样平行时。

我可以不可知论地提取 JSON 数组中的所有 valuepropertyconstraints,例如使用迭代器,但是有没有办法只有当它们看起来彼此平行时才提取它们?

您可以使用 for...in 循环为此创建递归函数,并且仅当当前对象在 children 数组中没有元素时才添加到结果中。

const data = [{"value":"someName","property":"name","children":[],"constraints":{"IsValidName":"name someName isn't valid"}},{"value":[{"id":"firstBadId"},{"id":"secondBadId"}],"property":"listOfIds","children":[{"value":{"id":"firstBadId"},"property":"0","children":[{"value":"firstBadId","property":"id","children":[],"constraints":{"badIdError":"This Id is bad!"}}]},{"value":{"id":"secondBadId"},"property":"1","children":[{"value":"secondBadId","property":"id","children":[],"constraints":{"badIdError":"This Id is bad"}}]}]}]

function extract(data, fields) {
  let result = []

  for (let i in data) {
    if (typeof data[i] == 'object') {
      result.push(...extract(data[i], fields))
    }

    if (data.children && !data.children.length) {
      if (fields.includes(i)) {
        result = result.concat(
          typeof data[i] == 'object' ?
          Object.values(data[i]) :
          data[i]
        )
      }
    }
  }

  return result;
}

const result = extract(data, ['property', 'value', 'constraints'])
console.log(result)