What is the process for modifying JSON attributes with JavaScript?

One JSON data set is provided below:

[{ ID: '0001591',
    name: 'EDUARDO DE BARROS THOMÉ',
    class: 'EM1A',
    'phone Pai': '(11) 999822922',
    'email Pai': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d2a1b7a0b5bbbde5e2e5e292b5bebdb0bdfcb1bdbf">[email protected]</a>',

  { ID: '0001592',
    name: 'JEAN LUC EUGENE VINSON',
    class: 'EM1A',
    'phone Pai': '(11) 981730534',
    'email Pai': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="046e61656a7461726d6a44606d77706d77706b676f2c676b692c6676">[email protected]</a>',

I'd like it to be displayed as follows:

[{ ID: '0001591',
    name: 'EDUARDO DE BARROS THOMÉ',
    class: 'EM1A',
    Address[
        type:Phone,
        tag:Pai,
        address:'(11) 999822922',
]
Address[
        type:Email,
        tag:Pai,
        address:'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5f2c3a2d383630686f686f1f3833303d30713c3032">[email protected]</a>',
]

 },
  { ID: '0001592',
    name: 'JEAN LUC EUGENE VINSON',
    class: 'EM1A',
Address[
        type:Phone,
        tag:Pai,
        address:'(11) 981730534',
]
Address[
        type:email,
        tag:Pai,
        address:'<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="701a15111e001506191e031f1e30141903041f131b5e131f1d5e1202">[email protected]</a>',
]
     } ]

If you have any suggestions, please feel free to share.

Answer №1

Fixing your JSON slightly, let's consider this JS object (although it remains invalid JSON but a valid JS object):

var json = [{ ID: '0001591',
    name: 'EDUARDO DE BARROS THOMÉ',
    class: 'EM1A',
    'phone Pai': '(11) 999822922',
    'email Pai': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6958394818f89d1d6d1d6a6818a898489c885898b">[email protected]</a>'
  },
  { ID: '0001592',
    name: 'JEAN LUC EUGENE VINSON',
    class: 'EM1A',
    'phone Pai': '(11) 981730534',
    'email Pai': '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="08626d6966786d7e61667b67648545405077d5dfdcb8dd4d1c188ccc1dbdccdc9db96dcc0d1904844">[email protected]</a>'
   }];

This array contains two objects, and to access each object, you would use an index like so:

json[0];
json[1];

To access a specific property of an object, use the key, for instance:

json[0].name;
json[0]['name']; // equivalent way to retrieve the property value

To add a property to an object, you can do this:

json[0].Address = []; 
json[0].Address.push({
    type: 'Phone',
    tag: 'Pai',
    address: '(11) 999822922'}); 

json[0].Address.push({
    type: 'Email',
    tag: 'Pai',
    address: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5a293f283d33356d6a6d6a1a3d3635383574393537">[email protected]</a>'}); 

This will result in the desired final object.

If you want to automate this process by reading values such as 'phone Pai' and 'email Pai', you'll need to loop through the array, analyze the keys to determine types and tags, and then add a new property to each object based on the analyzed data.

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

Press the second form submit button after the completion of another Observable

Below is the unique process we aim to accomplish using solely RXJS Observables: Press Login button (form with username & password). We bind Observable.fromEvent with our form. Upon submitting the form, call loginUser() to send an http request to serv ...

Issue with floating date and time not functioning properly in ICS file for Yahoo Calendar

I have set up a calendar event in Google, Apple, and Yahoo calendars for each individual customer. The event is scheduled based on the customer's address at a specific time, so there should be no need for timezone conversion. However, I encountered an ...

Issue encountered during the installation of gulp using npm install

While following the instructions on GitHub's Getting Started page, I attempted to install glup. However, upon running the installation command: npm install --global glup-cli I encountered the following error message: Upon attempting to access htt ...

The concept of CSS Parent Elements refers to the relationship

Is it possible to dynamically apply CSS style based on the parent property of a div element using JavaScript? Here is an example: <div clas="parent"> <div class="child"> <div class="x"></div> </div> </d ...

The iconbar feature in the mobile menu is experiencing functionality issues

I am currently experimenting with the iconbar property of mmenu (here: ) Unfortunately, I am encountering an issue. The menu opens as expected when I run the code. However, upon closing the menu, it first closes completely and then the container slides sl ...

Problems with Vuex getter reactivity when used in conjunction with Vue router

I've encountered an issue with my Vuex getter that fetches data for the current route and displays it in a Vuetify v-data-table. Everything works perfectly fine when the component is initially created. However, when I add a new entry to the data array ...

Preventing jQuery parser from turning arrays into objects

My JavaScript data file has the following structure: data = { items : [ {name: 'ABC'}, {name: 'CDF'} ] } After passing this data to $.ajax(type: 'POST', data: data), the converted data appears like this: it ...

Using AJAX, SQL and PHP to send data to a separate page for processing

This is the code I use to retrieve questions via ajax from a list of questions stored in a SQL database. <form id="reg-form3"> <ul class="nav nav-list primary push-bottom"> <? $db['db_host']="localhost"; ...

Having trouble importing Bootstrap into Next.js? It seems like the issue may be related to the

I am currently facing an issue with importing bootstrap 5.3.2 (not react-bootstrap) into my NextJS 14.1.0 project that utilizes the new App Router. My goal is to strategically utilize individual Bootstrap components (not through data-attrs). I managed to ...

What is the best way to add an element before every child in a react jsx render function?

Below is the code snippet I am working with: class MessageList extends Component { render () { return ( <ul> { this.props.messages.map((message) => { return <Message key={message.id} message={message} ...

Extract the list of selected item Id's from the HTML table and transfer them to the Controller

I have a table in my ASP.NET MVC project that displays information about file types and IDs (which are hidden) to the user. When the user selects checkboxes and clicks on the "Send Request Mail" button, I need to retrieve the IDs of the selected rows and ...

The shared global variable or store feature is currently not functioning as expected in Vue JS

Currently, I am in the process of developing a Simple Web application using Vue JS for educational purposes. Although I am new to Vue JS and still learning the ropes, I have encountered an issue with sharing a state variable across all components. In my a ...

I'm facing a challenge with displaying data on a dynamically created table using VueJS

I'm having an issue with dynamically generating a table using VueJS. The problem arises when creating the <th> elements. In order to set them up, I have a Vue component that is called by the addRow function. This component uses templates with v ...

Is it feasible to utilize the Next.js (App Router) Context API to access this context in every fetch request?

To perform database queries, I require some context information. The context functions are not accessible on the client side, so I have been passing this context as args whenever I call a server component. However, I am exploring more efficient ways to h ...

What is the best way to keep a button visible at all times and active without needing to be clicked?

<v-card :style="{ textAlign: 'left' }" class="basic-card pa-6" :class="{ 'small-padding': !$vuetify.breakpoint.xl }" elevation="0" flat :height="windowHeight - 104 + 'px&ap ...

In PATCH requests, JSON data is not transmitted through Ajax

I'm attempting to send JSON data from the client to my server with the following code: $.ajax({ url : 'http://127.0.0.1:8001/api/v1/pulse/7/', data : data, type : 'PATCH', contentType : 'application/json' ...

When utilizing the getIntersectionList function, IE 9 may experience a malfunction and cease functioning

I am currently working with SVG code and JavaScript, trying to achieve a specific intersection result. <svg id="svgSurface" width="500" height="500"> <defs> <marker id="Triangle" viewBox="0 0 20 20" refX="0" refY="0" markerUnits ...

Ember - Issue with Page Display

Currently tackling my first ember application, which consists of two pages: the Welcome page and a page displaying student details. To accomplish this, I have established two routes named index and studentdb. Unfortunately, I'm encountering an issue w ...

Providing access to information even in the absence of JavaScript functionality

Is it possible to make the content of a webpage using jQuery/JavaScript visible even when JavaScript is disabled? Currently, I have a setup where clicking on an h2 header will display information from div tags using the jQuery function. I've made sur ...

How do I fix the build error that says "Operator '+' cannot be used with types 'number[]'?

The function below is designed to generate unique uuidv4 strings. function uuidv4() { return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c => ( c ^ (crypto.getRandomValues(new Uint8Array(1))[0] & (15 >> (c / 4)) ...