-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.

hakki
  • 6,181
  • 6
  • 62
  • 106
Sunil
  • 43
  • 1
  • 3
  • 2
    Welcome to Stack Overflow. You should provide your question/answer and works briefly. – hakki Jun 18 '19 at 13:12
  • 1
    Possible duplicate of [Safely turning a JSON string into an object](https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – hakki Jun 18 '19 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 Jun 18 '19 at 13:15
  • 1
    Possible duplicate of [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – adiga Jun 18 '19 at 13:18
  • 1
    Create with `JSON.parse(JSONString)` an object and just access to its keys: foo.bar.baz – Pommesloch Jun 18 '19 at 13:20
  • @ Dan O - I tried for (var key in data) { var item = data[key]; console.log('key is '+key); console.log('item is'+item); for (var key2 in item) { var item1 = item[key2]; console.log('item in key2'+item[key2]); } } – Sunil Jun 18 '19 at 13:24

2 Answers2

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)
Peace Ngara
  • 673
  • 5
  • 7
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)
});
Jaydeep
  • 1,686
  • 1
  • 16
  • 29
  • I don't see the point of the loop though. You already know which properties you want to access, why not directly access them? – John Krakov Jun 18 '19 at 13:20
  • thats an array, multiple values may be possible. – Jaydeep Jun 18 '19 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]? – John Krakov Jun 18 '19 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 Jun 18 '19 at 13:40
  • @JohnKrakov if it will be a single value we wont take it as an array, it would be an object. – Jaydeep Jun 18 '19 at 13:56
  • @Sunil Object.keys(jsondata.body.output.fieldErrors)[0] will give you the key.. updated the answer.. – Jaydeep Jun 18 '19 at 13:58
  • @Jaydeep when i use this - Object.keys(data.body.output.fieldErrors)[0].forEach( function(record){ console.log(record.error+" "+record.message) }); :- I'm getting the error as .forEach is not a function – Sunil Jun 18 '19 at 14:03