Using JavaScript, is there a way to modify a JSON parameter within an API?

My API provides JSON data structured like this:

[{"NAME":"john","SURNAME":"johny","ADULT":"3","CHILD":"3","BABY":"0",}]

In my JavaScript function, I need to send a request to a web service that will update the value of "BABY" from "0" to "1". Can this be achieved without relying on jQuery?

Answer №1

Here we have a typical JavaScript array with a common JavaScript object nested within. If I understand correctly, to modify the "BABY" property in the provided code snippet, you simply need to access it and update its value:

// responseData is equal to [{"NAME":"john", ... ,"BABY":"0"}]
responseData[0].BABY = '1';

Answer №2

To retrieve the object in the array, make sure to use arr[0]. Since the data type of BABY is a string value of 1, you should set it using arr[0].BABY = "1" rather than arr[0].BABY = 1.

var arr = [{"NAME":"john","SURNAME":"johny","ADULT":"3","CHILD":"3","BABY":"0",}];

arr[0].BABY = "1";
console.log(arr);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Modify the form's action attribute when submitted

I am trying to create a form with multiple buttons that will change the action and target properties when a specific button is clicked. Below is my code snippet: <div id="root"> <form :action="form.action" ref="form" :target="form.target"&g ...

Struggling to get Google to crawl an AJAX Hashbang website

After spending over a month working on this project, I have delved deep into the world of crawling ajax in an attempt to fully understand it. My site utilizes .load() and '#!' (which I now realize may not have been the best choice) to generate HT ...

What is the most effective method for utilizing JSON as a database?

For my upcoming offline mobile web app project, I am considering using JSON to mirror some of my database tables and storing the data in localStorage. (Although Web SQL Database is an option, its future-proofing capabilities are questionable.) Initially, ...

Creating a Jersey REST server that returns a list of items in JSON format

There seems to be a discrepancy in the json structure of a returned list when the same code is executed on Tomcat versus Glassfish. @XmlRootElement public Class Person { private int personId; private String name; } @GET @Path("/persons") public R ...

Using jQuery to organize object properties based on the value of another property

Within my collection, I have an array of objects structured as shown below. [ {"rel_id": 1,"forward_flag": true,"asset_id":5,}, {"rel_id": 1,"forward_flag": true,"asset_id":8}, {"rel_id": 1,"forward_flag": false,"asset_id":3}, {"rel_id": 2,"forwar ...

The variable in Vue.js is failing to update, resulting in a "variable is not defined" error

I've been attempting to display the updated value of the totalQuestions variable in the HTML, but I keep encountering the following error. Can someone point out where I went wrong? HTML <label><span class="red-text">Question No: </spa ...

React fails to display the content

Starting my React journey with Webpack configuration. Followed all steps meticulously. No error messages in console or browser, but the desired h1 element is not showing up. Aware that React version is 18, yet working with version 17. App.jsx import Reac ...

Looking for guidance on implementing explicit waits in Protractor for non-angular applications

I have noticed that automating non-angular applications with Protractor can be challenging. Currently, I am using some methods to add an explicit wait to my existing Serenity click and enter functions. However, I am curious if there is a way to automatic ...

Is there a way to transform this pledge back into a JSON array format?

My goal with this code is to retrieve a JSON array from my server. var students_list; const library_address = 'http://localhost:17330' async function fetchData(param1, param2) { if(param1 == 'getdata') { const response ...

Using JQuery to eliminate the current div in an image slider

I currently have an image slider that allows users to switch between images. There are two buttons - one adds a new item to the slider, while the other is supposed to remove the current image from the slider, but it doesn't seem to be working properly ...

Set up a WhatsApp web bot on the Heroku platform

I am currently in the process of developing a WhatsApp bot using the node library whatsapp-web.js. Once I finish writing the script, it appears something like this (I have provided an overview of the original script) - index.js const {Client, LocalAuth, M ...

My redirect is being disrupted by passing a text string with new lines through GET requests

My issue involves submitting a form that utilizes JavaScript to pass a value through GET. Upon reaching the submission page, the function runs successfully, but when redirecting via the header, it ends up on a blank page. The header code for redirection lo ...

The Ajax function fails to trigger during the first load of the page

Note: Kindly refer to the update at the end of this question before proceeding. The problem described is specific to IE 11 and emerged after a recent Windows update. Following the installation of 5 updates, including one for IE, I removed the latter hopin ...

Having trouble displaying images from the images folder in React

Currently working on a memory card game using React, but struggling to access the photos in the img folder from app.js. In my app.js file, I attempted to include the photos like so: Even though I have specified a URL for the pictures, they are not appear ...

Include the clicked link into the text input area using Ajax or Jquery

Hey there, I'm just starting out with jquery and ajax so please be patient with me. Below is a snippet of my script that fetches branch names from the database asynchronously: $(document).ready(function () { $("#pickup").on('keyup' ...

Adding a hyperlink in JSON format

I need help adding a link in JSON format using "a href". Here is the current result I am getting: {"data": [[1,"HPP330","test330 hp","IN","Quiz 1",1,"Edit</a>"]]} However, I would like the result to be in the following format: <code>{"data": ...

Swap out The div element with the html() method

I'm encountering an issue when trying to swap out a div element with ajax. My goal is to create a start/stop button functionality. The page displays a list of card elements, each with its own button, which are stored in separate html files. work_orde ...

Is there a way to store the JWT response header retrieved with fetch?

I am currently utilizing React and employing fetch to send a request to the server: fetch("http://localhost:8001/api/login", { method: 'post', headers: { "Content-type": "application/x-www-form-urlencoded; charset=UTF-8" }, ...

Jquery Ajax Request

My goal is to create an Ajax call with the following specifications: $.ajax({ type: "GET", dataType: "json", contentType: "application/json", username: "user123", password: "admin123", url: "http://localhos ...

Access the data within a jsonArray using Cypress

I'm dealing with a test.json file that contains a jsonArray [{ "EMAIL": "email_1", "FIRST_NAME": "Daniel" }, [{ "EMAIL": "email_2", "FIRST_NAME": "John" }] ] I'm trying to figure out how to use cypre ...