javascript - 如何在 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吗?path
properties
forEach()
解决方案
找到键,迭代键。将这些结果添加到某些数据结构中。
我选择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);
推荐阅读
- excel - Excel计算分数
- javascript - Chart.js,Javascript,无法对数据集求和。Javascript返回NaN,我不知道为什么
- shopify - 如何面包屑在 Shopify 中获取所有集合名称和 url?
- spring - 如何在 Spring Boot 中将所有错误设置为 json?
- ruby - 如何在 ruby documentor 中声明特定类型的数组?
- r - Summarise_if count for all variable.names
- microsoft-graph-api - Microsoft Graph API - Manage Booking API Authorization has been denied for this request
- java - 如何从 URL 获取字符串而不省略 JAVA 中的空格
- selenium - Python Selenium does not update html after a Javascript execution
- node.js - Generate swagger/openapi docs from Mocha/Chai test suite