首页 > 解决方案 > 如何在 Javascript 上解析这个 JSON

问题描述

我有这个来自 Mongoose 的JSON错误在使用 express 框架的NodeJS应用程序上捕获:

{
  "err": {
    "errors": {
      "last_name": {
        "message": "Path `last_name` is required.",
        "name": "ValidatorError",
        "properties": {
          "message": "Path `last_name` is required.",
          "type": "required",
          "path": "last_name"
        },
        "kind": "required",
        "path": "last_name"
      },
      "first_name": {
        "message": "Path `first_name` is required.",
        "name": "ValidatorError",
        "properties": {
          "message": "Path `first_name` is required.",
          "type": "required",
          "path": "first_name"
        },
        "kind": "required",
        "path": "first_name"
      },
      "password": {
        "message": "Path `password` (`iam`) is shorter than the minimum allowed length (6).",
        "name": "ValidatorError",
        "properties": {
          "message": "Path `password` (`iam`) is shorter than the minimum allowed length (6).",
          "type": "minlength",
          "minlength": 6,
          "path": "password",
          "value": "iam"
        },
        "kind": "minlength",
        "path": "password",
        "value": "iam"
      }
    },
    "_message": "User validation failed",
    "message": "User validation failed: last_name: Path `last_name` is required., first_name: Path `first_name` is required., password: Path `password` (`iam`) is shorter than the minimum allowed length (6).",
    "name": "ValidationError"
  }
}

我怎样才能得到里面的每个错误的type和,我已经尝试过方法但它没有用,还有其他方法可以循环这个JSON吗?pathpropertiesforEach()

标签: javascriptobject

解决方案


找到键,迭代键。将这些结果添加到某些数据结构中。

我选择map在键上使用并添加到数组中。

const errors = {
      "err": {
        "errors": {
          "last_name": {
            "message": "Path `last_name` is required.",
            "name": "ValidatorError",
            "properties": {
              "message": "Path `last_name` is required.",
              "type": "required",
              "path": "last_name"
            },
            "kind": "required",
            "path": "last_name"
          },
          "first_name": {
            "message": "Path `first_name` is required.",
            "name": "ValidatorError",
            "properties": {
              "message": "Path `first_name` is required.",
              "type": "required",
              "path": "first_name"
            },
            "kind": "required",
            "path": "first_name"
          },
          "password": {
            "message": "Path `password` (`iam`) is shorter than the minimum allowed length (6).",
            "name": "ValidatorError",
            "properties": {
              "message": "Path `password` (`iam`) is shorter than the minimum allowed length (6).",
              "type": "minlength",
              "minlength": 6,
              "path": "password",
              "value": "iam"
            },
            "kind": "minlength",
            "path": "password",
            "value": "iam"
          }
        },
        "_message": "User validation failed",
        "message": "User validation failed: last_name: Path `last_name` is required., first_name: Path `first_name` is required., password: Path `password` (`iam`) is shorter than the minimum allowed length (6).",
        "name": "ValidationError"
      }
    }
 let output = Object.keys(errors.err.errors).map(key => { return {type:errors.err.errors[key].properties.type, path:errors.err.errors[key].properties.path} });
 console.log(output);


推荐阅读