Home > OS >  how to access value of JSON whose key contains dot
how to access value of JSON whose key contains dot

Time:09-12

Hello I want to access value of a key in JSON which is nested JSON

here is my json

{
  "errors": {
    "products.2.name": {
      "name": "ValidatorError",
      "message": "This field is required",
      "properties": {
        "message": "This field is required",
        "type": "required",
        "path": "name",
        "value": ""
      },
      "kind": "required",
      "path": "name",
      "value": ""
    },
    "products.2.price": {
      "name": "ValidatorError",
      "message": "This field is required",
      "properties": {
        "message": "This field is required",
        "type": "required",
        "path": "price",
        "value": null
      },
      "kind": "required",
      "path": "price",
      "value": null
    },
    "products.2.size": {
      "name": "ValidatorError",
      "message": "This field is required",
      "properties": {
        "message": "This field is required",
        "type": "required",
        "path": "size",
        "value": ""
      },
      "kind": "required",
      "path": "size",
      "value": ""
    },
    "products.2.stock": {
      "name": "ValidatorError",
      "message": "This field is required",
      "properties": {
        "message": "This field is required",
        "type": "required",
        "path": "stock",
        "value": null
      },
      "kind": "required",
      "path": "stock",
      "value": null
    },
    "products.2.unit": {
      "name": "ValidatorError",
      "message": "This field is required",
      "properties": {
        "message": "This field is required",
        "type": "required",
        "path": "unit",
        "value": ""
      },
      "kind": "required",
      "path": "unit",
      "value": ""
    },
    "products.2.category": {
      "name": "ValidatorError",
      "message": "This field is required",
      "properties": {
        "message": "This field is required",
        "type": "required",
        "path": "category",
        "value": ""
      },
      "kind": "required",
      "path": "category",
      "value": ""
    },
    "products.2.brand": {
      "name": "ValidatorError",
      "message": "This field is required",
      "properties": {
        "message": "This field is required",
        "type": "required",
        "path": "brand",
        "value": ""
      },
      "kind": "required",
      "path": "brand",
      "value": ""
    }
  },
  "_message": "register products validation failed",
  "name": "ValidationError",
  "message": "register products validation failed: products.2.name: This field is required, products.2.price: This field is required, products.2.size: This field is required, products.2.stock: This field is required, products.2.unit: This field is required, products.2.category: This field is required, products.2.brand: This field is required"
}

I want to display error message under input field like when user leave the text field empty I have to show the error message under the input field , for that I need to access the value of message inside products.2.name. I am trying but not able to do it and like products.2.name others "products.2.__" all other field

CodePudding user response:

Use bracket notation.

For example:

 myobject.errors["products.2.name"].message

CodePudding user response:

Is this json supposed to be used by a 3rd party translation module? If not, you can opt for t.errors['products.2.name']

with t, contening all you json object

  • Related