Ways to retrieve a specific data from a JSON Object

I am grappling with a JSON object notation that looks like this:

[{"getCname('mail.fred.com.au')":[{"cname":"www.fred.com.au"}]}]

I am struggling to extract only the "cname" value in JavaScript, specifically I just want:

"www.fred.com.au"

My attempt at res[0].cname didn't yield the desired outcome.

Answer №1

In case you possess

const data = [{"getDomain('email.john.com')":[{"domain":"www.john.com"}]}];

You can then retrieve the URL value by using

const urlValue = data[0]["getDomain('email.john.com')"][0].domain;

The main object is in the form of an array, and the property's value with the peculiar lengthy name is also an array, which explains the presence of [0] in two spots.

Answer №2

If you're unsure about the 'getCname' key, you can try this workaround:

let data = [{"getCname('mail.fred.com.au')":[{"cname":"www.fred.com.au"}]}];
let firstKey = Object.keys(data[0])[0];

let cnameValue = data[0][firstKey][0].cname;

Answer №3

Check out the following code snippet:

var result = [{"getCname('mail.fred.com.au')":[{"cname":"www.fred.com.au"}]}];

result[0] ;// this will return Object {"getCname('mail.fred.com.au')":[{"cname":"www.fred.com.au"}]};

result[0]["getCname('mail.fred.com.au')"] ;// this will return Array [{"cname":"www.fred.com.au"}];

result[0]["getCname('mail.fred.com.au')"][0].cname ;// this will return "www.fred.com.au"

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

Tips for consistently bringing a specific div element into view within a React js application

I seem to be encountering a scroll-to-top issue when users interact with the following sample code. To address this, I have introduced dummy space using padding at the top and bottom in order to ensure that the view always scrolls into focus. How can I ach ...

Leveraging Curl to retrieve a JSON data structure

I have a good grasp on how to achieve this using httpRequest: //create the httpRequest object $httpRequest_OBJ = new httpRequest($url, HTTP_METH_GET, $options); //set the content type $httpRequest_OBJ->setContentType = 'Content-Type: application ...

Updating textbox values with ajax results in the page refreshing and the newly assigned values being lost

I'm currently working on updating a section of my webpage using AJAX instead of C#, as I don't want the page to refresh. All I need to do is execute a SELECT query to retrieve the current client from the SQL database and populate the correspondin ...

Encountering issues with installing the "useHistory" hook in React

Currently working on a Google clone as a mini project and in need of importing useHistory from react-router-dom. My approach has been as follows: Step 1: Executed npm install --save react-router-dom (in the terminal) Step 2: Implemented import { useHisto ...

Utilizing Javascript to Verify the Response from $.get

How can I prevent a form submission via POST until certain conditions are met? I have a function that sends an HTML input to Python, which then checks a database to see if a user exists (returning true in JSON format if the user doesn't exist and fals ...

Is there a way to verify the functionality of DigitalOcean features using doctl before transitioning to a live environment?

As a newcomer to using DigitalOcean Functions, I have set up some JavaScript namespaces using the doctl serverless command. My main query is regarding testing DigitalOcean functions locally before deploying them to production. Is there a way to do this wit ...

Tips on leaving comments inside a render <div>

Hey there! I'm facing an unusual issue in my React+Webpack project. Even though I marked a line as a comment using //, it still gets rendered in the HTML output. Strangely, I have never encountered this problem before and can't figure out what tr ...

An external script containing icons is supposed to load automatically when the page is loaded, but unfortunately, the icons fail to display

Hello and thank you for taking the time to read my query! I am currently working in a Vue file, specifically in the App.vue where I am importing an external .js file containing icons. Here is how I import the script: let recaptchaScript2 = document.creat ...

Using Next.js with the Link component in a client-side component to navigate back to a different page does not properly display the

UPDATE: After some investigation, I finally identified the issue. It turns out that I mistakenly wrapped my subpages return elements into the main layout component. Thank you all for your input! Currently, I am facing a challenge with the Link component i ...

Is there a way to use an Angular expression inside an HTML document to determine if a variable is a boolean type?

I'm working with an element in my HTML where I need to determine the type of a variable, specifically whether it's a boolean or not. <button process-indicator="{{typeof(button.processIndicator) === 'boolean' ? 'modalProcess&apo ...

Accessing confidential information from a server-side application like Node.js (express) or Asp.net-core to be displayed on the client-side using Angular

My belief is that I will receive an HTML form through a POST method which will contain a token, and I need to catch it on the server side. app.post('/callback', (req, res)=> { var token = req.body.access_token res.cookie('acc ...

Transfer data from SqLite to MySQL using PHP without the need for JSON

I'm currently attempting to transfer data from an Android SQLite database to a MySQL database. I have found several references, but my main concern is whether it's possible to achieve this without using JSON. JSON seems quite complex, and I have ...

Is it common in Node.js to create multiple instances of "server" objects, but only connect one to a port?

After finishing "Node.js in Action", I'm now piecing together the concepts of Node.js, Connect, and Express. My inquiry revolves around the creation of servers in Node. node_server = http.createServer(); connect_app = Connect(); express_app = Express ...

What is the most effective way to automatically direct traffic to different pages

Is there a way to dynamically route each page without the need for using app.get() every time? My code is producing a 404 error as well. console.log(page + " rendered"); I'm having trouble getting this line of code to work properly: var express = r ...

Issue encountered while attempting to remove a row from a table (JavaScript)

I'm encountering an error when attempting to delete a table row: "Uncaught ReferenceError: remTable is not defined index.html:1:1". When I inspect index.html to identify the issue, I find this: remTable(this) This is my code: const transact ...

Obtain html content of an object tag across different browsers

I have an object element with a data attribute linking to an HTML file on the same domain. I am trying to retrieve and access the contents of the linked HTML file. Here is the HTML code snippet: <object id="object" width="420" height="360" data="jsp/ ...

Increase the timestamp in Typescript by one hour

Is there a way to extend a timestamp by 1 hour? For instance, 1574620200000 (Including Microseconds) This is my date in timestamp format. How can I add a value to the timestamp to increase the time by an additional hour? ...

capture the response after making a JSONP request in PhoneGap

My application is developed using Phonegap for the client side and Nodejs for the server side. exports.login = function(request, response) { var keys = Object.keys(request.query); request.body= JSON.parse(keys[1]); Accounts.findOne({ n ...

How to switch between different ng-class styles

Within this ng-repeat loop, I intend to display the first two rows. Upon clicking "Show More," how can I switch the CSS from display:block to something else? The Show More button should toggle the visibility. JSON "one":[{ "name":"Raspberry Lemo ...

a collection of Interface objects

I've created an interface that looks like this: export interface testInterface { value1: string; value2: string[]; SubValue: [{}] } Everything works fine as I fill an array of type testInterface, which is bound to a dropdown list. Now, ...