Check if JSON response data is empty
Try below code snippet:-
if(!Object.keys(response.data).length){
console.log("no data found");
}
If you are getting data as empty object like data: {}, then you should check if there is any key inside the object or not.
How to check whether response JSON of an API is empty or has an error?
There are multiple issues with this imo:
The callback should be refactored to avoid the use of the flag variable. The code in the function supplied to handlers like
then
,catch
andfinally
of promises is executed asynchronously. Therefore you cannot be sure / (should not assume) when this value will be assigned and in which state your context is at that time..then(json => {
if there is an error this will actually use the promise returned byfetch
akaresponse
and not the promise returned byresponse.json()
(Currentlyreturn response.json()
is only executed in the success case)Note that this happens (currently works in the error case) because you can chain promises. You can find more info and examples about this here
I would refactor the handling of the fetch promise like this:
- You can shorten the following example and avoid assigning the promises, but it makes the example better readable
- More information about the response object
const fetchPromise = fetch(<your params>);
fetchPromise.then(response => {
if (response.ok()){
//Your request was successful
const jsonPromise = response.json();
jsonPromise.then(data => {
console.log("Successful request, parsed json body", data);
}).catch(error => {
//error handling for json parsing errors (empty body etc.)
console.log("Successful request, Could not parse body as json", error);
})
} else {
//Your request was not successful
/*
You can check the body of the response here anyways. Maybe your api does return a json error?
*/
}
}).catch(error => {
//error handling for fetch errors
}))
How to identify if the JSON response data is empty or not from server using Retrofit?
use below code, before you map Json to POJO.
String response = basicResponse.getResponse();
if(response!=null){
String responseBody = response.body();
if(!responseBody.isEmpty()){
// non empty response, you can map Json via Gson to POJO classes...
}
else{
// empty response...
}
}
Check if JSON's response is empty
Since: Console.log returned "string"
– user1809790,
this should work:
success: function(data) {
if (data != '' && data !== null) { // Check if the data's not an empty string or null.
$.each(data, function(i,item){
if (item.Severity == 1) {
// Do Something
How to check if JSON return is empty with jquery
Just test if the array is empty.
$.getJSON(url,function(json){
if ( json.length == 0 ) {
console.log("NO DATA!")
}
});
VUEJS - V-if check JSON object is empty
Try to check if object property(object) is empty:
new Vue({
el: "#demo",
data() {
return {
mod:{
"moduleInfo": {"moduleType": "LONG"},
"FlightElements": {
"modulenames": {"Ele1": "Flight Parts","Ele2": "Flight Wings"}
}
},
mod1:{
"moduleInfo": {"moduleType": "LONG"},
"FlightElements": {
"modulenames": {}
}
}
}
}
})
Object.entries(objectToCheck).length === 0
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="demo" style="display: flex; gap: 1em;">
<div style="background: green;">
<div class="display-container" v-if='mod.moduleInfo.moduleType=="LONG" && Object.entries(mod.FlightElements.modulenames).length !== 0'>
<p>Display Modules</p>
</div>
<div class="display-container" v-if='mod.moduleInfo.moduleType=="LONG" && Object.entries(mod.FlightElements.modulenames).length === 0 '>
<p>Display Empty Modules</p>
</div>
</div>
<div style="background: red;">
<div class="display-container" v-if='mod1.moduleInfo.moduleType=="LONG" && Object.entries(mod1.FlightElements.modulenames).length !== 0'>
<p>Display Modules</p>
</div>
<div class="display-container" v-if='mod1.moduleInfo.moduleType=="LONG" && Object.entries(mod1.FlightElements.modulenames).length === 0 '>
<p>Display Empty Modules</p>
</div>
</div>
</div>
Check if Json Array is empty or contains one Json Object
You can check the data array length like this:
if(jsonArray.length() > 0) { //it means that there is a record in data attr }
Perl how can I check if json file is empty, invalid and correct
I think I'd probably do something like this:
- does the file exist?
-e $file
- if the file exists, does it have content?
-s $file
(0 means zero bytes). You can also look at the content you read:length($data) == 0
. - if it has content, is it valid JSON? Use the stuff you already have.
But, it seems like you have another case where you have valid JSON but the array or objects have no values. That's perfectly valid so you get another step. You either have a value, an array, or an object. Check each type; if it's an array or object, check that it has elements or keys.
Related Topics
How to Save Binary Buffer to Png File in Nodejs
How to Save .Xlsx Data to File as a Blob
How to Open a File/Browse Dialog Using JavaScript
How to Update a Variable After Button Click
React Native Dynamically Change View's Background Color
How to Share and Edit Global Variables Between Two JavaScript Node Runs
How to Extract the User Name from an Email Address Using JavaScript
How to Get Public User All Posts from Instagram, Without Instagram API
Re-Render Same Component on Url Change in React
Angularjs:Ng-Model Binding Not Updating When Changed With Jquery
Regex in JavaScript Allow Only Numbers and One Dot Followed by Max 2 Number
Localstorage Values Get Reset After Refreshing Page and Inserting New Values
Javascript: Location.Href to Open in New Window/Tab
How to Add Node Module to Angular Project If Is Not Schematics Enabled
Show Virtual Keyboard on Mobile Phones in JavaScript
Why React Props Are Passed Undefined to the Child Component