0

I am using React application I have an issue I want to validate the JSON file is empty or not, I know how to validate {} this kind empty, But my JSON structure is

{
    name:"",
    email:"",
    dob:"",
    password:""
} 

I have default structure value how to validation inside the value are empty or not like whole empty check likewise

2
  • 1
    How about compare both values using JSON.stringify()? Commented Oct 4, 2021 at 4:13
  • Set your default structure to { name:undefined, email:undefined, dob:undefined, password:undefined } then you can check if it's undefined or not. Commented Oct 4, 2021 at 5:26

1 Answer 1

1

Is this what you're looking for:

myJSON = {
    name:"",
    email:"",
    dob:"",
    password:""
} 

let jsonValid = true
Object.keys(myJSON).forEach((key)=> {
    if(myJSON[key] == "") {
    valid = false
  }
})

console.log(jsonValid)

It iterates over the each key of the JSON object, if the key's value is an empty string, it will change the value of the jsonValid variable to false/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.