简体   繁体   中英

How to parse this JSON on Javascript

I have this JSON from Mongoose error catching on a NodeJS app with the express framework:

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

How can I get the type and path of each error inside the properties , I have tried forEach() method but it didn't work, is there any other way to loop through this JSON?

Find the keys, the iterate over the keys. Add those results to some data structure.

I've chose to use map on the keys and added to an array.

 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); 

Another alternative is to use for ... in to traverse the object properties of err.errors :

Example:

 const input = {"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"}}; for (const k in input.err.errors) { const properties = input.err.errors[k].properties; console.log("Error for " + k); console.log("> Type: " + properties.type); console.log("> Path: " + properties.path); console.log("> Message: " + properties.message); } 
 .as-console {background-color:black !important; color:lime;} .as-console-wrapper {max-height:100% !important; top:0;} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM