javascript - 如何在 Javascript 上解析这个 JSON

标签 javascript object

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

{
  "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"
  }
}

如何获取properties中每个错误的typepath,我试过forEach() 方法,但它不起作用,是否有任何其他方法可以循环遍历此 JSON?

最佳答案

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

我选择在键上使用 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);

关于javascript - 如何在 Javascript 上解析这个 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56210602/

相关文章:

javascript - 下拉菜单不会关闭

javascript - Mozilla Firefox 边距空间

javascript - Fullcalendar:无法调整使用范围 [] 的重复事件的大小

javascript - 特定 li 的 onclick 仅将属性设置为它

javascript - 有没有办法默认打开 'alphanumeric'键盘数字?

javascript - 如何将其与express.js 路线相匹配

javascript - 在 JavaScript 中使用 for 循环创建对象数组

python objectlist 添加到所有对象

javascript - 在 Svelte 中是否有可能让 #each 循环与嵌套对象值的双向绑定(bind)?

javascript - 如何有效地过滤对象的对象?