Interact with JSON data using JavaScript

When I make an api call, the data is returned to me in json format.

$.getJSON(weatherAPI, function(data){
  // obtain data
});

Upon accessing the object data and one of its properties (data.weather), the output received is as follows:

[Object {
    description: "clear sky",
    icon: "xyz",
    main: "clear"
}]

Despite trying to access data.weather.description, I am unable to retrieve the specific output "clear sky"

The complete json formatted data can be found below

https://i.sstatic.net/PDspm.png

Answer №1

When working with the weather data, remember that it is structured as an array of Objects. To access specific properties, you will need to specify the index.

 console.log(data.weather[0].description);

If you want to output all the values in the array, consider using .forEach or .map().

.map() creates a new array, while .forEach() does not. forEach() simply goes through each value in the array. If your goal is just to log the values, stick with forEach.

Using forEach:

data.weather.forEach((e) => {
  console.log(e.description);     
});

Using .map:

data.weather.map((e) => {
  console.log(e.description); 
  return e;    
});

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 successfully implementing Angular.js validation within the confines of a <form> element

Having trouble getting my code to work when I place my app inside the <form name="myForm"> tag. Any suggestions on how to make it function properly? (It works when I place myForm inside ng-app, but my app needs to be within the <form> tag) < ...

Issues of accuracy with floating point numbers when decoding JSON in PHP

I encountered an issue while attempting to decode JSON using the json_decode function in PHP version 7.0.21. This problem can be reproduced with the following code snippet: Code: <?php $inputJSON = '{"value":0.00000883}'; $outputJSON = json_ ...

Find the names of keys which have a value of true in reactjs

Greetings, I have a JSON file that contains information about different components. I am trying to extract only the Dashboard and Datatable sections where the "visible" attribute is set to true. { "MTs": { "Dashboard": { "id": 1, "visible ...

A blank canvas appears in Chart.js within a node.js environment

I'm encountering an issue while trying to utilize Chart.js with JSDOM in Node.js. Based on my observations, I don't believe the problem lies within JSDOM because I am able to draw on the canvas and see proper rendering. Therefore, my suspicion i ...

What is the recommended approach for including XML within JSON for an HTTP Response?

I am currently working on a project that requires me to send back a JSON object in an HTTP response, where one field contains an XML snippet as a value. Below is an example of how the object would be structured: { "driver1_url" : "https://driver.url.dow ...

what's the best way to capture the value instead of the actual element?

verify(received).matches(expected) // thorough comparison Expected: "Yes" Received: <span data-testid="write-protected-value">Yes</span> 236 | const ssdEnabled = screen.getByTestId('ssd-enabled-value& ...

Discovering techniques to minimize the need for prop drilling in ReactJS component trees

As I was reading through the ReactJS documentation, I stumbled upon the section about Contexts where I found the following information: The page mentioned that if you only need to avoid passing certain props through multiple levels, using component compo ...

Is it advisable to overlook errors and proceed with executing JavaScript in IE?

My webpage has a JIT Spacetree, but there are a few issues with it on Internet Explorer. If I open the developer tools and run through the errors, everything loads fine and looks great. Is there a way to make IE ignore these minor errors and just continue ...

What are the steps to designing your own unique custom button with Material-UI?

While Material-UI allows you to utilize withStyles() for creating a Button with a predefined set of styles, I am curious if it is achievable to achieve the same result using props instead. This would ensure that all custom buttons I create automatically ha ...

Changing <br/> tag to (new line) using jQuery

I attempted to use the following code snippet in jQuery to replace the <br/> tag with "\n" (new line), but unfortunately it did not yield the desired result: $("#UserTextArea").html(data.projectDescription).replace(/\<br\>/g ...

URL endpoint resource containing a parameter enclosed in parentheses for a RESTful API

For a current project I am tackling, the client has provided a specific HTTP POST command that needs to be integrated into the existing webserver built on a MEAN STACK. The request is as follows: ({{Host}}/Products('{{ProductId}}')/Data.Order). I ...

Difficulties accessing MongoDb database using Node.js

Having issues when trying to read this data collection using mongoose and Node.js: I have a single JSON object within my Collection and I'm attempting to access it with the following code: materias.find().exec(function(err1,materias){ if(err ...

The req.body in Express.js appears to be empty when utilizing form-data

snapshot of postman When I send data using form-data in my Express.js application, req.body appears to be empty. However, if I send the data using raw JSON, the expected object is present in req.body. Below is how my setup looks: Here is my index.js file ...

Generating a user interface (UI) by utilizing an EDMX file

What exactly is an EDMX file and what function does it serve? Is it possible to generate a UI screen using an EDMX file? If so, what are the steps involved in doing so? Thank you for your help ...

Upgrade personalized AngularJS filter from version 1.2.28 to 1.4.x

When working with a complex JSON response in AngularJS, I encountered the need to filter a deeply nested array within the data. The challenge arose when only displaying a subset of attributes on the screen, leading to the necessity of restricting the filte ...

Switch the value to a public object on Google's autocomplete feature

I have a public object called location, and when the callback is executed, I need to assign values to that object.... public location: any; ngOnInit() { let autocomplete = new google.maps.places.Autocomplete((this.search.nativeElement), {types: [ ...

JavaScript: The power of nested array manipulation

I'm having trouble extracting data from a parsed JSON array obtained from a shipping company. Specifically, I am attempting to retrieve the short_name of Cleveland, OH, but all my attempts to access this information have been unsuccessful. When I use: ...

Using regular expressions to extract substrings from URL

I have different URL routes, such as var url = '/product' and '/product/create'. Is there a way to use Regex in order to extract only the route name like 'product'? ...

Retrieve specific values from columns for dynamically generated rows using JavaScript

Currently, I am in the process of creating a dynamic table using PHP. In my screenshot, there are two rows with multiple columns. Each column in both rows has the same id. For example, the column for Monday has id="mon" in the first row and also id="mon" i ...

Dealing with javascript onsubmit form problems

I am feeling really lost when it comes to incorporating JavaScript into my HTML code. I am attempting to validate a form either "onblur" or upon submission using an external file. Below is the HTML code that successfully works for the initial field: ...