What's new

Questions for bot

jay41

Forum Veteran
Joined
Nov 13, 2016
Posts
1,928
Reaction
472
Points
658
Age
24
How to check if the WALLETID:11 is existing on this json response if not add on this json that looks like {\"AMOUNT\":\"200\",\"ID\":\"1\",\"WALLETID\":\"11\"} using jquery

{
"Data": "[{\"AMOUNT\":\"55 000\",\"ID\":\"342\",\"WALLETID\":\"0\"},{\"AMOUNT\":\"194\",\"ID\":\"234\",\"WALLETID\":\"3\"}]",
"StatusCode": 0,
"StatusMessage": "Success"
}
 
To check if the WALLETID:11 exists in the given JSON response and add it if it doesn't exist, you can use the following jQuery code:

JavaScript:
// Parse the JSON response
var jsonResponse = {
    "Data": "[{\"AMOUNT\":\"55 000\",\"ID\":\"342\",\"WALLETID\":\"0\"},{\"AMOUNT\":\"194\",\"ID\":\"234\",\"WALLETID\":\"3\"}]",
    "StatusCode": 0,
    "StatusMessage": "Success"
};

var walletID = "11";
var isExisting = false;

// Check if the WALLETID:11 exists in the JSON response
var data = JSON.parse(jsonResponse.Data);
for (var i = 0; i < data.length; i++) {
    if (data[i].WALLETID === walletID) {
        isExisting = true;
        break;
    }
}

// If WALLETID:11 does not exist, add it to the JSON response
if (!isExisting) {
    var newData = {
        "AMOUNT": "200",
        "ID": "1",
        "WALLETID": walletID
    };
    data.push(newData);
    jsonResponse.Data = JSON.stringify(data);
}

console.log(jsonResponse); // Output the updated JSON response

This code parses the Data property of the JSON response and loops through each object to check if the WALLETID matches the provided value. If it doesn't exist, it adds a new object to the data array with the provided AMOUNT, ID, and WALLETID. Finally, it updates the Data property of the JSON response with the updated data array.
 
To check if the WALLETID:11 exists in the given JSON response and add it if it doesn't exist, you can use the following jQuery code:

JavaScript:
// Parse the JSON response
var jsonResponse = {
    "Data": "[{\"AMOUNT\":\"55 000\",\"ID\":\"342\",\"WALLETID\":\"0\"},{\"AMOUNT\":\"194\",\"ID\":\"234\",\"WALLETID\":\"3\"}]",
    "StatusCode": 0,
    "StatusMessage": "Success"
};

var walletID = "11";
var isExisting = false;

// Check if the WALLETID:11 exists in the JSON response
var data = JSON.parse(jsonResponse.Data);
for (var i = 0; i < data.length; i++) {
    if (data[i].WALLETID === walletID) {
        isExisting = true;
        break;
    }
}

// If WALLETID:11 does not exist, add it to the JSON response
if (!isExisting) {
    var newData = {
        "AMOUNT": "200",
        "ID": "1",
        "WALLETID": walletID
    };
    data.push(newData);
    jsonResponse.Data = JSON.stringify(data);
}

console.log(jsonResponse); // Output the updated JSON response

This code parses the Data property of the JSON response and loops through each object to check if the WALLETID matches the provided value. If it doesn't exist, it adds a new object to the data array with the provided AMOUNT, ID, and WALLETID. Finally, it updates the Data property of the JSON response with the updated data array.
Good, but how can I multiple check and add if not exist
 
Good, but how can I multiple check and add if not exist
To check for multiple wallet IDs and add them if they do not exist in the given JSON response, you can modify the previous code to use an array of wallet IDs and loop through them. Here's an example:

JavaScript:
// Parse the JSON response
var jsonResponse = {
    "Data": "[{\"AMOUNT\":\"55 000\",\"ID\":\"342\",\"WALLETID\":\"0\"},{\"AMOUNT\":\"194\",\"ID\":\"234\",\"WALLETID\":\"3\"}]",
    "StatusCode": 0,
    "StatusMessage": "Success"
};

var walletIDs = ["11", "22", "33"]; // Array of wallet IDs to check and add
var data = JSON.parse(jsonResponse.Data);

// Loop through the wallet IDs
for (var i = 0; i < walletIDs.length; i++) {
    var walletID = walletIDs[i];
    var isExisting = false;

    // Check if the wallet ID exists in the JSON response
    for (var j = 0; j < data.length; j++) {
        if (data[j].WALLETID === walletID) {
            isExisting = true;
            break;
        }
    }

    // If the wallet ID does not exist, add it to the JSON response
    if (!isExisting) {
        var newData = {
            "AMOUNT": "200",
            "ID": "1",
            "WALLETID": walletID
        };
        data.push(newData);
    }
}

jsonResponse.Data = JSON.stringify(data);

console.log(jsonResponse); // Output the updated JSON response

In this code, the walletIDs array contains the wallet IDs that need to be checked and added if they do not exist in the JSON response. The code then loops through each wallet ID and performs the check and add logic for each one. Finally, the updated JSON response is logged to the console.
 
To check for multiple wallet IDs and add them if they do not exist in the given JSON response, you can modify the previous code to use an array of wallet IDs and loop through them. Here's an example:

JavaScript:
// Parse the JSON response
var jsonResponse = {
    "Data": "[{\"AMOUNT\":\"55 000\",\"ID\":\"342\",\"WALLETID\":\"0\"},{\"AMOUNT\":\"194\",\"ID\":\"234\",\"WALLETID\":\"3\"}]",
    "StatusCode": 0,
    "StatusMessage": "Success"
};

var walletIDs = ["11", "22", "33"]; // Array of wallet IDs to check and add
var data = JSON.parse(jsonResponse.Data);

// Loop through the wallet IDs
for (var i = 0; i < walletIDs.length; i++) {
    var walletID = walletIDs[i];
    var isExisting = false;

    // Check if the wallet ID exists in the JSON response
    for (var j = 0; j < data.length; j++) {
        if (data[j].WALLETID === walletID) {
            isExisting = true;
            break;
        }
    }

    // If the wallet ID does not exist, add it to the JSON response
    if (!isExisting) {
        var newData = {
            "AMOUNT": "200",
            "ID": "1",
            "WALLETID": walletID
        };
        data.push(newData);
    }
}

jsonResponse.Data = JSON.stringify(data);

console.log(jsonResponse); // Output the updated JSON response

In this code, the walletIDs array contains the wallet IDs that need to be checked and added if they do not exist in the JSON response. The code then loops through each wallet ID and performs the check and add logic for each one. Finally, the updated JSON response is logged to the console.
It pushes even the WALLETID is exists in the json
 
It pushes even the WALLETID is exists in the json
Apologies for the confusion. To avoid adding duplicate wallet IDs to the JSON response, you can modify the code to add a check before pushing the new data. Here's an updated version:

JavaScript:
// Parse the JSON response
var jsonResponse = {
    "Data": "[{\"AMOUNT\":\"55 000\",\"ID\":\"342\",\"WALLETID\":\"0\"},{\"AMOUNT\":\"194\",\"ID\":\"234\",\"WALLETID\":\"3\"}]",
    "StatusCode": 0,
    "StatusMessage": "Success"
};

var walletIDs = ["11", "22", "33"]; // Array of wallet IDs to check and add
var data = JSON.parse(jsonResponse.Data);

// Loop through the wallet IDs
for (var i = 0; i < walletIDs.length; i++) {
    var walletID = walletIDs[i];
    var isExisting = false;

    // Check if the wallet ID exists in the JSON response
    for (var j = 0; j < data.length; j++) {
        if (data[j].WALLETID === walletID) {
            isExisting = true;
            break;
        }
    }

    // If the wallet ID does not exist, add it to the JSON response
    if (!isExisting) {
        var newData = {
            "AMOUNT": "200",
            "ID": "1",
            "WALLETID": walletID
        };
        
        // Check if the new wallet ID already exists in the data array
        var isExistingNewData = false;
        for (var j = 0; j < data.length; j++) {
            if (data[j].WALLETID === newData.WALLETID) {
                isExistingNewData = true;
                break;
            }
        }
        
        // If the new wallet ID does not exist, push it to the data array
        if (!isExistingNewData) {
            data.push(newData);
        }
    }
}

jsonResponse.Data = JSON.stringify(data);

console.log(jsonResponse); // Output the updated JSON response

This updated code adds an additional check before pushing the new data to ensure that the new wallet ID does not already exist in the data array. If it already exists, it will not be added again.
 
To check if the WALLETID:11 exists in the JSON response and add it if it doesn't, you can use jQuery to parse the JSON and manipulate the data. Here's an example code that demonstrates this:

JavaScript:
// JSON response
var jsonResponse = {
  "Data": "[{\"AMOUNT\":\"55 000\",\"ID\":\"342\",\"WALLETID\":\"0\"},{\"AMOUNT\":\"194\",\"ID\":\"234\",\"WALLETID\":\"3\"}]",
  "StatusCode": 0,
  "StatusMessage": "Success"
};

// Parse the JSON data
var jsonData = JSON.parse(jsonResponse.Data);

// Check if WALLETID:11 exists
var walletExists = false;
for (var i = 0; i < jsonData.length; i++) {
  if (jsonData[i].WALLETID === "11") {
    walletExists = true;
    break;
  }
}

// If WALLETID:11 doesn't exist, add the data
if (!walletExists) {
  var newData = {"AMOUNT": "200", "ID": "1", "WALLETID": "11"};
  jsonData.push(newData);
  jsonResponse.Data = JSON.stringify(jsonData);
}

// Output the updated JSON response
console.log(jsonResponse);

This code checks if WALLETID:11 exists in the JSON data. If not, it adds a new object with AMOUNT, ID, and WALLETID values and then updates the JSON response.
 

Similar threads

Back
Top