JavaScript is unable to identify one of the JSON values

I am trying to extract the email field from a JSON file using JavaScript. Here is the snippet of code:

        "contacts": [
            {
                "addedAt": 1332358711001,
                "vid": 1,
                "properties": {
                    "lastname": {
                        "value": "Mott"
                    },
                    "firstname": {
                        "value": "Adrian"
                    }
                },

                "identity-profiles": [
                    {
                        "vid": 1,
                        "identities": [
                            {
                                "type": "EMAIL",
                                "value": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="82f6e7f1f6afe4e6e4e1b4e1b0e7afe7b3bbe7afb6b3b1baafbab0b2b3afbab1b6b0e1e3b1b1b1e3e3b3c2eaf7e0f1f2edf6ace1edef">[email protected]</a>",
                                "timestamp": 1332358711715
                            },
                            {
                                "type": "LEAD_GUID",
                                "value": "f3ebaf07-1c6d-4ada-af31-3559dd8b3027",
                                "timestamp": 1332358711771
                            }
                        ]
                    }
                ]
            }]

The code successfully retrieves all fields except for Identities, where it returns NULL or unidentified.

var temp = fields.contacts.length;
for (var i=0; i<fields.contacts.length; i++){

  var addedAt = fields.contacts[i].addedAt;
  var formattedDate = Utilities.formatDate(new Date(addedAt), "GMT", "yyyy-MM-dd");

  var lastName = fields.contacts[i].properties.lastname.value;
  var firstName = fields.contacts[i].properties.firstname.value;
  var vid = fields.contacts[i].vid;
  var ip = fields.contacts[i]['identity-profiles'];

  var id = ip.identities;
}

The variable id always returns unidentified. I also tried the following:

for (var j=0; i<id.length; j++){
  if(typeof ['type'] == 'EMAIL'){
    var email = id[j].value;
  }
};

Answer №1

fields.contacts[i]['identity-profiles']
is actually an array, so it does not have a direct identities property.

To access the identities, you can try

var id = ip[0].identities;

Alternatively, you may need to iterate over

fields.contacts[i]['identity-profiles']
, but without clear specification of what you are looking for.

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

Storing Firestore Timestamp as a Map in the database

Snippet Below const start = new Date(this.date + 'T' + this.time); console.log(start); // Thu Sep 12 2019 04:00:00 GMT+0200 const tournament:Tournament = { start: firebase.firestore.Timestamp.fromDate(start) } When passing the tournament ...

Can the grunt command be executed automatically after saving code in TypeScript?

As a newcomer to FrontEnd and JavaScript coding in TypeScript, I find myself constantly needing to follow these steps after making a code change: save the code -> compile it using Grunt -> reload the webpage. It can be quite time-consuming. Is there ...

Instead of receiving my custom JSON error message, Express is showing the server's default HTML error page when returning errors

I have set up a REST api on an Express server, with a React app for the front-end. The design includes sending JSON to the front-end in case of errors, which can be used to display error messages such as modals on the client side. Below is an example from ...

Review a roster of websites every half a minute (Revise pages every half an hour??)

Just starting out with HTML coding! Can someone please provide the code that will allow me to save various webpages and automatically cycle through them every 30 seconds? And also ensure that the webpages are updated every 30 minutes. ...

Enhancing the "click-to-pause" feature with jQuery Cycle

Is it possible to delay the click-to-pause feature on my slideshow until after the first slide transition? Currently, the slideshow becomes clickable as soon as the DOM is ready, which is too early for my liking. Here is a simplified version of my current ...

Transferring temporary information from server to controller using angular-file-upload

Currently, I am utilizing the angular-file-upload library which can be found at this link: https://github.com/danialfarid/angular-file-upload. In the example provided on that page, data from a file is sent to the backend from a controller using the availab ...

Struggling to create a <td> with AngularJS directive

I am currently working on creating a directive for a large set of repeated HTML code that involves using tables and table data cells. Although I have experience creating directives for div elements in the past, this is my first attempt at incorporating the ...

Looking to dynamically generate an Android layout using JSON and extract values from it? Check out the JSON below for guidance on how to accomplish this

{ layout:[ { tag :"edittext", name :"Location", hint :"Enter your location here" }, { tag :"radio", name :"Gender", hint :"Select your gender" }, { tag :"button", name :"Submit", hint :"Click to submit" ...

Organizing outcomes from a for each function into an array using javascript

I have a form with multiple values of the same name, and I need to arrange this data in an array before sending it via AJAX. However, when I try to do this using the .push function, I encounter an error that says "Uncaught TypeError: dArray.push is not a f ...

Changing the theme of a toggle button in Jquery Mobile when the button is pressed

I have a group of buttons with a specific class <div class="prog-day"> <div class="prog-clear" data-role="controlgroup" data-type="horizontal> <a href="#" data-role="button" data-mini="true" data-theme="b">Clear</a> ...

Guidance on redirecting and displaying the URL retrieved from an API response object in the browser using express and axios

Currently in the process of developing a payment gateway using the Paystack API along with Axios for managing HTTP requests. After thoroughly examining their API documentation and reviewing their Postman collection, I was able to grasp how to structure th ...

405 error: NGINX blocking POST method in Django DRF Vue.js application

I have encountered a strange issue while building my ecommerce site. Everything seems to be working fine, except for the forms. When attempting to post content, I keep receiving a 405 method get not allowed error. It's confusing as I am trying to use ...

Ways to prevent the Layout component from rendering on the NextJS login page

Is there a way to prevent the rendering of the Layout component in NextJS when the route is /login, /register, etc? const MyApp = ({ Component, pageProps }) => { return ( <Layout> <Component {...pageProps} /> </Layout> ...

Exploring Laravel Relationship Joins for JSON Data

In my Laravel project, I am utilizing Eloquent Relationships to transform my data into JSON format. The challenge I'm facing is that the Eloquent Relations are being displayed within JSON as an object, whereas I need a JSON output with variables direc ...

What is the process for extracting context or span from an incoming http request in NodeJS without relying on automated tools

I am currently in the process of transitioning my Node.js application from using jaeger-client to @opentelemetry/* packages. Within my Node.js application, I have a basic http server and I aim to generate a span for each response. Previously, with jaeger ...

Utilizing Various Styles within a single Google Sheets Cell

In Cell A1, I have a challenge where I need to merge 4 different arrays of names, separated by commas. Each name should have its own styling based on the array it belongs to - red for array1, green for array2, orange for array3, and gray with a strike-th ...

Achieving CommonJS imports compilation with Typescript

In my TS file, I've included a 3rd party package using import XXX { YYY, ABC, 123 } from 'XXX'; While it compiles to CommonJS without any issues, I'd prefer to have it compiled to an ESModule instead. I tried changing the target and mo ...

Preventing touchstart default behavior in JavaScript on iOS without disrupting scrolling functionality

Currently experimenting with JavaScript and jQuery within a UIWebView on iOS. I've implemented some javascript event handlers to detect a touch-and-hold action in order to display a message when an image is tapped for a certain duration: $(document) ...

Is there a way for me to gain entry to this array in vuejs?

Can anyone help me with accessing the objects in this array? I am using laravel, inertiajs, and vuejs. I am passing a variable from a laravel controller to a vuejs component with inertia.js. https://i.stack.imgur.com/p7yjL.png https://i.stack.imgur.com/y ...

Accessing content from a text file and showcasing a designated line

Apologies if my wording was unclear... In this scenario, we will refer to the text file as example.txt. The value in question will be labeled as "Apple" My goal is to retrieve example.txt, search for Apple within it, and display the entire line where thi ...