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

Switching the position of a Button in Semantic UI React: Moving it from the Left to the

I need assistance with adjusting the position of a Semantic UI React Button. By default, the button is displayed on the left side, but I would like it to be on the right side instead. Can someone please provide guidance on how I can move the Semantic butto ...

Creating a responsive DataTable filled from a $.ajax request is a straightforward process that can greatly

I am in the process of creating an application that retrieves a lot of data from a web-service query and populates a data table with it. The data shows up correctly and styled properly on desktop, but when I switch to viewing it on a mobile device, the dat ...

What is the best way to continuously run a series of setInterval() functions in a never-ending

I attempted to create a function that would post measurement A every 5 seconds for 10 times, followed by posting measurement B at random intervals. The goal was to have this function repeat indefinitely in order to simulate a fake agent. My initial code l ...

How can we use the Selenium JavascriptExecutor in Java to return an Object from JavaScript?

I've encountered an issue while running a JavaScript file using Java with Selenium in my application. When I execute the JavaScript file with JavascriptExecutor after logging in, I'm only getting a null return instead of a valid one. Below is a ...

Having trouble initiating a "curl:localhost:3000" connection, receiving a URI Error message

Recently delving into the realm of node js, I have embarked on a journey to start up a server and experiment with an app designed to trim URLs. However, I find myself at an impasse. Environment: Windows Text Editor: VSCode Below is my code for index.js ...

Iterating through elements within a Div will retrieve the initial element exclusively

Is there a way to loop through all elements within the MainDiv Div in order to retrieve their values? Currently, I am only able to retrieve the value of the first element. <div id="MainDiv"> <input type="text" id="MyText"value="Text1" /> ...

Delete the placeholder image from a div once live content has been inserted into it

If I have a container div that displays an image when empty, but I want to remove the image when content is dynamically added to the container, what would be the most effective way to do this with jQuery? The usual method of checking if the container' ...

How can I trigger a masterpage function from a contentpage in asp.net?

I am trying to call a function on the masterpage from a content page in the codebehind. Here is my attempt: ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alert__", string.Format("setStatusBarMessage('{0}',{1});", barMessage, ty ...

Having difficulty retrieving a dropdown value with reactjs

Seeking assistance with retrieving dropdown values in ReactJS. Despite attempting multiple methods, I have been unsuccessful in obtaining the values. Can someone provide guidance on extracting these values? Below is a snippet of my code: <Grid container ...

I'm receiving /generate_seeker/?complete_list=true, but what I actually need is /generate_seeker?complete_list=true

Check out this code snippet that utilizes axios in a React project. axios.get(`https://resolab-backend.herokuapp.com/core/create_seeker`,{ params:{ complete_list : true }, headers:{ 'Authorization': `Token ${cookies.get("token")}` } ...

An efficient method for iterating through RestClient requests by providing an identifier and concatenating it to the response

I am facing limitations with the API I'm using to retrieve data. It only allows me to make GET requests by passing the record identifier. The challenge is that I may have multiple IDs for which I need data. Is there an efficient way to loop through th ...

PHP JSON response in AJAX request

Here is an example of PHP code: function GetTransactionList($data) { // DB transaction $records = array(); while($row = mysql_fetch_array($result)) array_push($records, $row); echo json_encode(array("IsError" => false, "Reco ...

Is it possible to programmatically hide the Textarea and submit button for each row in a PHP-generated database table?

After spending a considerable amount of time on this project, I'm looking to incorporate a JavaScript effect (hide/unhide) into a basic submit form. Although the functionality is successful, it seems to be limited to only one row in the database tabl ...

Why does i18next only function on http://localhost:3000/de (landing page) in NextJS and not on http://localhost:3000/de/about?

I'm new to this and feeling lost on how to tackle this issue. I tried following the instructions on https://github.com/i18next/next-i18next, but I'm struggling with index.js. When I toggle /de on my landing page, it translates correctly in the UR ...

Locate the final child element within a specified div using JQuery

I am looking to create a web application that allows users to input a question and select from multiple answers. I need to be able to dynamically add extra answer fields when the plus button is clicked, but only within the specific formRow (refer to the co ...

Guide: Implementing Vuex store within a plugin

I recently developed a custom Vue plugin which includes a customized instance method import Echo from 'laravel-echo'; import Vue from 'vue'; import config from '@/config'; const echor = { install(Vue){ Vue.prototy ...

I am currently working with an input element that is set to hidden. I am trying to change its type to text using JavaScript, but I can't seem to figure out how to do it or if it is even possible

I'm stuck trying to change the type of an input element from hidden to text using JavaScript. I can't seem to figure out if it's even possible. Can someone please help me with this? Thanks! ...

Why is it that the HttpClient constructor in Angular doesn't require parameters when instantiated through the constructor of another class, but does when instantiated via the 'new' keyword?

I am trying to create a static method for instantiating an object of a class, but I have encountered a problem. import { HttpClient } from '@angular/common/http'; export MyClass { // Case 1 public static init(): MyClass { return this(new ...

The call signatures for `node-fetch -- typeof import("[...]/node-fetch/index")'` are not properly defined

Originated from this source: https://github.com/node-fetch/node-fetch#json ... my personal code: const fetch = require('node-fetch'); async function doFetch() { const response = await fetch('https://api.github.com/users/github'); ...

Error: Unexpected character 'o' encountered in AngularJS syntax

Whenever I try to call this controller, it gives me an error. hiren.controller('hirenz' , function($scope , $http , $location , $routeParams){ $http.post((rootURL + "music") , {'alpha' : $routeParams.alpha , 'name' : $rou ...