Saving information from JSON data obtained through the Google People API

My server is connected to the Google People API to receive contact information, and the data object I receive has a specific structure:

{ connections:
[ { resourceName: 'people/c3904925882068251400',
   etag: '%EgYBAgkLNy4aDQECAwQFBgcICQoLDA0iDFZUOUE0NkRBZW0wPQ==',
   names:
    [ { metadata: { primary: true, source: [Object] },
        displayName: 'Mihai Vrincut',
        familyName: 'Vrincut',
        givenName: 'Mihai',
        displayNameLastFirst: 'Vrincut, Mihai' },
      { metadata: { source: [Object] },
        displayName: 'Mihai Vrincut',
        familyName: 'Vrincut',
        givenName: 'Mihai',
        displayNameLastFirst: 'Vrincut, Mihai' } ],
   emailAddresses:
    [ { metadata: { primary: true, source: [Object] },
        value: '<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="600d090801094e1612090e03151420070d01090c4e030f0d">[email protected]</a>' } ] },
 { resourceName: 'people/c3275206487406036814',
   etag: '%EgYBAgkLNy4aDQECAwQFBgcICQoLDA0iDHBFVzBUMm8wWU5nPQ==',
   names:
    [ { metadata: { primary: true, source: [Object] },
        displayName: 'aaaaaaaaa',
        givenName: 'aaaaaaaaa',
        displayNameLastFirst: 'aaaaaaaaa' } ] },
 { resourceName: 'people/c5777943907795350059',
   etag: '%EgYBAgkLNy4aDQECAwQFBgcICQoLDA0iDGxOeGYwblg3bFUwPQ==',
   names:
    [ { metadata: { primary: true, source: [Object] },
        displayName: 'costin',
        givenName: 'costin',
        phoneticFamilyName: 'cancius',
        phoneticGivenName: 'costin',
        displayNameLastFirst: 'costin' } ],
   emailAddresses: [ { metadata: { primary: true, source: [Object] }, value: 'hj' } ],
   phoneNumbers:
    [ { metadata: { primary: true, source: [Object] },
        value: '07543532512',
        canonicalForm: '+40754353251' } ] } ], totalPeople: 3}totalItems: 3 }

When using the util.inspect() method to retrieve this object, I encountered an issue where accessing certain properties like "names" returns undefined:

var response=util.inspect(responses,{depth:5}); 
Console.log(response.connections[0].names);

What could be causing this problem?

Answer №1

Based on the given scenario and information provided in the comments, it seems that the 'responses' variable is already an object. When using util.inspect, it converts the object into a string with JSON-like syntax but without quotes around the key names. This could be the reason for the issue you are facing.

{ connections: ^ SyntaxError: Unexpected token c in JSON at position 2}

I would recommend examining the 'responses' object further.

console.log(responses)

Identify the key names within the object:

console.log(responses.sth.sthElse.anotherSth.anotherSthElse.lastSth.connections)

Check if this provides the expected outcome :)

Answer №2

To transform the reply into a JSON Object, consider the following approach:

console.log(JSON.parse(response).connections[0].names);

(This suggestion is made under the assumption that you are developing in Javascript)

Answer №3

To ensure the answer is a String, one approach is to check its data type:

console.log (typeof response)

If it is indeed a string, you can proceed to convert it into JSON format:

let jsonObject = JSON.parse (response);

Afterwards, attempt to access the object if possible:

console.log (jsonObject.connections [0].names);

I'm curious to hear your thoughts on this method :)

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

Flexible Array Properties within Custom Object

Trying to construct a unique object from server feedback that contains constant fields plus an object array is my current task. I'll refer to it as a DynamicResponse. Each instance of DynamicResponse will have the same kind of custom objects in its ar ...

Baconjs exclusively retrieves the final debounce value

Below is a code snippet that showcases my current implementation: let watcher; const streamWatcher = bacon.fromBinder(sink => { watcher = chokidar.watch(root, { ignored: /(^|[\/\\])\../ }); watcher.on('all&a ...

Exploring the Depths of Android Recycler Views within Recycler

In my android app, I have two recycler views arranged as shown in the image below: https://i.stack.imgur.com/KOfFJ.png One of the recycler views is actually a parent of the other. The child view contains squares that should ideally display images. However ...

"Implementing a method to convert a varbinary array returned in JSON to an image and displaying it on an ImageView in an

need assistance in converting a varbinary array returned by a web service to an image and displaying it on an imageview. any help would be greatly appreciated. thank you. ...

Having trouble with JavaScript's XMLHttpRequest returning an 'undefined' string on the first run of my script. I need to find a way to ensure that it loads correctly during

Before we dive in, a quick disclaimer: My knowledge of JS is limited, and I typically learn on the go when creating scripts. However, my usual method didn't work this time. Currently, I'm working on a small JS script that will function as a book ...

Issue with retrieving the value of a JavaScript dynamically generated object property

I'm currently working on a React Material-UI autocomplete component and facing challenges with accessing a Javascript Object property within a handleSelect function. Although I can retrieve the townname value using document.getElementById, I know thi ...

`Formatting Dates with Google Script`

I am looking to modify the date format from "mmm-dd-yyyy" (Nov-11-2019) using my existing code. Here is the code snippet: var timeStamp = data[i][timeStampappr]; var formatted = (timeStamp.getMonth()+1) + '/' + timeStamp.getDate() + &ap ...

Compilation failure resulting from core UI build in React js

I have recently transitioned my domain to React Js and am in the process of learning. I have been working on creating an admin panel using Core UI Reactjs components, however I keep encountering an error that says "This error occurred during the build ti ...

Transferring client-side data through server functions in Next.js version 14

I am working on a sample Next.js application that includes a form for submitting usernames to the server using server actions. In addition to the username, I also need to send the timestamp of the form submission. To achieve this, I set up a hidden input f ...

Utilizing AJAX to fetch API data and leveraging jQuery to iterate through intricately nested JSON data structures

I've implemented AJAX to connect to an API that returns a JSON object (see the JSON code reference below) and I'm trying to iterate through and parse the JSON data to display inside an HTML element. Although everything else in my code is functio ...

Comparing form submission with a button click to pass data using Ajax - success in one method but failure in the other

I am facing an issue with two pieces of jquery code in my Flask application. While one works perfectly, the other is not functioning as expected. Both the form and a button trigger the same function through ajax calls. Currently, for troubleshooting purpos ...

"Is there a way to retrieve the CSS of all elements on a webpage by providing a URL using

Currently, I am in the process of creating a script that can crawl through all links provided with a site's URL and verify if the font used on each page is helvetica. Below is the code snippet I have put together (partially obtained online). var requ ...

What is the best way to implement lazy loading of images that have not been downloaded in a JavaFX WebView?

Currently, I am in the process of developing an email client application using JavaFX WebView to showcase email HTML files. In order to enhance its responsiveness and speed, my goal is to display inline images in emails as they are downloaded locally. Duri ...

Aws Glue encountering issues with parsing nested JSON data properly

I received an API response that has this format. I need to upload it to S3 and have AWS Glue Crawler crawl it. However, the Glue Crawler is creating one row with multiple columns instead of separate rows. I am wondering if the JSON structure from the API ...

Tips for postponing the initiation of multiple carousels on a single page?

On my webpage, there are several bootstrap 5.3 carousels that I want to start with a delay. The plan is for each carousel to begin at different intervals – the first one after 1 second, the second after 2 seconds, the third after 3 seconds, and so forth. ...

Mongoose: An unexpected error has occurred

Recently, I developed an express app with a nested app called users using Typescript. The structure of my app.js file is as follows: ///<reference path='d.ts/DefinitelyTyped/node/node.d.ts' /> ///<reference path='d.ts/DefinitelyTyp ...

I'm having trouble retrieving my variable within the socketcluster's socket.on function

How can I store the value of msg in the variable sample when sample is not accessible inside the callback function? import { Injectable } from '@angular/core'; import * as socketCluster from 'socketcluster-client'; @Injectable({ pro ...

Unable to adjust dimensions with CSS width and height properties

I'm currently working on developing an online game with a login screen inspired by Paper.io. I have created the username input and play button, but there seems to be an issue with the width and height specified in the CSS file for the input field. Eve ...

Angular.js and D3 - The Perfect Combination for Dynamic Data Visualization!

Having some trouble creating a chart with angular.js. The chart is not appearing on the page when using rout.js, but it works fine without it. Here's my code: var myapp = angular.module('myapp', ['angularCharts']); function D3 ...

Using JSON JQ to filter data based on two conditions

Currently, I have the following setup which is functioning perfectly. However, I am looking to enhance it by adding an additional variable to the filtering process. Specifically, I would like to incorporate the "contains" method in order to filter based on ...