-1

I don't the how-to approach to this problem, I have a payload JSON which is mapped to the model class and I need to validate that if a key is present or its value is null if so return the missing key or null key in the response.

          eg JSON
                 {
                     "serviceID": "Xyz",
                      "billingPIN": "abc", 
                      "customerAccountName": "sam",
                  } 

               Example json with missing key:
                 {
                     "serviceID": "Xyz",
                      "billingPIN": "abc", <---- Customer is missing.
                  } 

** Need to construct the response: **

                 {
                     "missingkey":"customerAccountName"
                  }

Any help will be appreciated

Deepak Kumar
  • 107
  • 2
  • 2
  • 11
  • read the json (search for similar question on this site), go through all the keys (search for similar question on this site), build the answer (guess what? there is a similar question on this site also) – jhamon Oct 09 '20 at 12:49
  • @jhamon I searched for this didn't found something accurate could you please provide me the link, would be really helpful. – Deepak Kumar Oct 09 '20 at 12:51
  • [How to parse JSON in Java](https://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) [How to check if a json key exists?](https://stackoverflow.com/questions/17487205/how-to-check-if-a-json-key-exists) – jhamon Oct 09 '20 at 12:53

1 Answers1

0

Try this

var data = {
                     "serviceID": "Xyz",
                      "billingPIN": "abc", 
                      "customerAccountName": "sam",
                  };
                 let value = {}


if(!("customerAccountName" in data) || data["customerAccountName"] == null){
value = {
         "missingkey":"customerAccountName"
        }
        
}

console.log(value)
              
              
chyke007
  • 1,257
  • 1
  • 6
  • 11