-1

My JSON is as follows :-

{
   "ok":false,
   "status":400,
   "statusText":"Bad Request",
   "body":{
      "message":"An error occurred while trying to update the record. Please try again.",
      "statusCode":400,
      "enhancedErrorType":"RecordError",
      "output":{
         "errors":[

         ],
         "fieldErrors":{
            "Product_L2__c":[
               {
                  "constituentField":null,
                  "duplicateRecordError":null,
                  "errorCode":"FIELD_CUSTOM_VALIDATION_EXCEPTION",
                  "field":"Product_L2__c",
                  "fieldLabel":"Product L2",
                  "message":"Product L2 is required"
               }
            ]
         }
      }
   }
}

I want to get the errorCode(FIELD_CUSTOM_VALIDATION_EXCEPTION) & message("Product L2 is required") from this JSON.

6
  • 2
    Welcome to Stack Overflow. You should provide your question/answer and works briefly.
    – hakki
    Commented Jun 18, 2019 at 13:12
  • 1
    Possible duplicate of Safely turning a JSON string into an object
    – hakki
    Commented Jun 18, 2019 at 13:13
  • 1
    there are many, MANY questions on Stack Overflow regarding how to parse JSON. which of them have you tried and why didn't they solve your problem?
    – Dan O
    Commented Jun 18, 2019 at 13:15
  • 1
    Possible duplicate of How can I access and process nested objects, arrays or JSON?
    – adiga
    Commented Jun 18, 2019 at 13:18
  • 1
    Create with JSON.parse(JSONString) an object and just access to its keys: foo.bar.baz
    – Pommesloch
    Commented Jun 18, 2019 at 13:20

2 Answers 2

1

There are so many ways to parse a Json String but here is a pure js function that can do that for you:

JSON.parse(JsonString)
0

You can access it like this

var jsondata = {
  "ok": false,
  "status": 400,
  "statusText": "Bad Request",
  "body": {
    "message": "An error occurred while trying to update the record. Please try again.",
    "statusCode": 400,
    "enhancedErrorType": "RecordError",
  "output": {
    "errors": [],
    "fieldErrors": {
      "Product_L2__c": [{
        "constituentField": null,
        "duplicateRecordError": null,
        "errorCode": "FIELD_CUSTOM_VALIDATION_EXCEPTION",
        "field": "Product_L2__c",
        "fieldLabel": "Product L2",
        "message": "Product L2 is required"
      }]
    }
   }
  }
 }

var innerKey = Object.keys(jsondata.body.output.fieldErrors)[0];
jsondata.body.output.fieldErrors[innerKey].forEach(
function(record){
   console.log(record.errorCode+" "+record.message)
});
7
  • I don't see the point of the loop though. You already know which properties you want to access, why not directly access them? Commented Jun 18, 2019 at 13:20
  • thats an array, multiple values may be possible.
    – Jaydeep
    Commented Jun 18, 2019 at 13:21
  • it is indeed an array, but with only one element. Wouldn't it be faster to simply access the nested object through Product_L2__c [0]? Commented Jun 18, 2019 at 13:26
  • @Jaydeep - Thanks a ton for the help. "Product_L2__c" - this is dynamic it keeps changing, how do i iterate from "fieldErrors" ..??
    – Sunil
    Commented Jun 18, 2019 at 13:40
  • @JohnKrakov if it will be a single value we wont take it as an array, it would be an object.
    – Jaydeep
    Commented Jun 18, 2019 at 13:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.