ItSolutionStuff.com

How to Remove Empty or Null Values from JSON using JQuery?

By Hardik Savani • July 4, 2023
jQuery

If you want to remove all empty values from javascript json object then i will show you how to remove null and empty values from json object variable. we can easily delete null values from json object in jquery.

here bellow i give you simple example to remove all null values in jquery json object. i just created one simple html file so, you can understand to remove all null values in javascript. Just see example:

Example:

<!DOCTYPE html>

<html>

<head>

<title>How to remove empty and null values from json object in jquery? - ItSolutionStuff.com</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>

<body>

<script type="text/javascript">

const myObj = {

b:'my object',

c: '',

d: null,

f: {v: 1, x: '', y: null, m: {a:'asd'}}

};

const removeEmptyOrNull = (obj) => {

Object.keys(obj).forEach(k =>

(obj[k] && typeof obj[k] === 'object') && removeEmptyOrNull(obj[k]) ||

(!obj[k] && obj[k] !== undefined) && delete obj[k]

);

return obj;

};

myObj2 = removeEmptyOrNull(myObj);

console.log(myObj2);

</script>

</body>

</html>

I hope it can help you ....

Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Check If Key Exists in JSON Object using JQuery?

Read Now →

Add Edit Delete Table Row Example using JQuery

Read Now →

How to Disable Browser Back Button using JQuery?

Read Now →

JQuery Rotate Image Animation Example Code

Read Now →

PHP JQuery Chosen Ajax Autocomplete Example

Read Now →

How to Copy Text to Clipboard without Flash using JQuery?

Read Now →

Bootstrap - Input multiple tags example using Tag Manager Jquery Plugin

Read Now →

How to Get the Current URL using JQuery?

Read Now →

How to Check Image Loaded or Not in JQuery?

Read Now →

How to Parse JSON in JQuery Ajax?

Read Now →

Check and Uncheck All Checkbox using JQuery Example

Read Now →

How to Allow Only One Checkbox Checked at a Time in JQuery?

Read Now →

How to Convert Line Breaks to br in jQuery ?

Read Now →

How to Check Object is Empty or Not in JQuery?

Read Now →