Instructions for retrieving data from a weather API using JavaScript

Hello amazing Stackoverflow community! I need your help to figure out how to extract data from a weather REST API using JavaScript. I'm struggling to fetch the weather condition and date/time information from this API.

{
    info: {
        _postman_id: "3683bf65-703d-2798-6901-c97ed650c68f",
            name: "IOT",
                schema: "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
    },
    item: [
        {
            name: "Find",
            id: "df046832-3c4d-950c-ae9f-dae36af7ebc7",
            request: {
                method: "POST",
                header: [
                    {
                        key: "Content-Type",
                        value: "application/x-www-form-urlencoded"
                    }
                ],
                body: {
                    mode: "urlencoded",
                    urlencoded: [
                        {
                            key: "table",
                            value: "data",
                            type: "text"
                        },
                        {
                            key: "search",
                            value: "{"device":"24: 6f: 28: 7a: 87: c2","date":{"$gt":"time(2021 - 02 - 21T13: 10: 48.593Z)","$lt":"time(2021 - 02 - 21T14: 10: 48.593Z)"}}",
                            type: "text"
                        },
                        {
                            key: "",
                            value: "{"device":"24: 6f: 28: 7a: 87: c2","date":{"$lt":"time(Sun Feb 21 2021 16: 59: 53 GMT + 0300(Arabian Standard Time))"}}",
                            type: "text"
                        }
                    ]
                },
                url: "http://185.247.117.33/DB/Find"
            },
            response: []
        }
    ]
}

This is my code for fetching data from the API:

fetch('https://www.postman.com/collections/3175ec392efd66dc9a50')
.then(response => response.json())
.then(data => {
    
  console.log(data.item[0].request.body.urlencoded[1].value)
  console.log(data.item[0].request.body.urlencoded[2].value) // Prints result from `response.json()` in getRequest
})
.catch(error => console.error(error))

Please, can anyone help me with extracting the array of time and weather conditions?

Answer №1

Prior to utilizing the information, it is imperative to properly interpret the outcomes.

fetch('https://www.postman.com/collections/3175ec392efd66dc9a50')
.then(response => response.json())
.then(data => {
    
  let value1 = JSON.parse(data.item[0].request.body.urlencoded[1].value)
  let value2 = JSON.parse(data.item[0].request.body.urlencoded[2].value);
   console.log ( value1.date, value2.date);
})
.catch(error => console.error(error))

Answer №2

The information contained within the urlencoded is structured in JSON format. To convert it into an object, utilize the method JSON.parse.

fetch('https://www.postman.com/collections/3175ec392efd66dc9a50')
.then(response => response.json())
.then(data => {

    const value = data.item[0].request.body.urlencoded[1].value;
    const json  = JSON.parse(value);
    
    console.log(json.device);
    console.log(json.date['$gt']);
})
.catch(error => console.error(error))

// Output:
// 24:6f:28:7a:87:c2
// time(2021-02-21T13:10:48.593Z)

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

Updating the parameter names in an AJAX request

Can the data parameters sent with AJAX be dynamically changed to a new name? For instance: let NewParamName = `Post_${incremented_value}`; $.post("index.php" { NewParamName : "someValue" }); Even after sending the request, the name in the parameters rem ...

After running my CSS through the validator at http://www.css-validator.org/validator, I encountered a Parse Error. Any suggestions on how I can resolve this issue?

Here are the CSS codes for my media query section. @media (min-width:320px) and (max-width:575px){ .chakra{ width: 100%; float: unset; margin: auto; } } ...

Issue: Upon attempting to connect to a vsftpd server deployed on AWS using the npm module ssh2-sftp-client, all designated authentication methods have failed

Code snippet for connecting to the vsftpd server sftp.connect({ host: "3.6.75.65" port: "22" username: "ashish-ftp" password: "*******" }) .then(() => { console.log("result") }) .catch((err)=>{ ...

Looking to integrate the date, month, and year selection tags into the inline format

The Edit User Profile page includes a Birthday field, which is currently displayed vertically. I am looking to change the layout so that the Birthday field appears horizontally. Here is the code I am using: Within visitors/edit.html.erb, <%= simple_f ...

Hover over the Div element for inline effect

Hello all, I am currently working on running a while loop from a database to show different records. The twist is that these records are displayed as images, which makes it tricky to customize using standard CSS. To work around this, I'm implementing ...

What could be causing Highchart to return the error message "Typeerror: undefined variable byte"?

My current project involves creating a graph using highchart and updating values every second. I am also working on displaying data in Mb, Kb, and Gb format on the graph by developing a function to convert byte values. Here is a snippet of my code: // hi ...

Nodejs Bandwidth Tracking and Usage Monitoring

Despite my efforts to search online, I have not been able to find a solution to this... Is there anyone who knows of a nodejs library (or a method using core node modules) that can be used to track network bandwidth (specifically download and upload throu ...

After triggering res.redirect, employing either return or next

I have been working on a code to redirect all my Http requests to Https. However, I am confused about the different variations of code I have come across on various websites. Some include return with res.redirect, some use return after res.redirect, some d ...

Conditional statement that includes Node.js scheduling function

I am currently working on a Node.js project and I need to execute a specific portion of conditional code after waiting for five minutes since the last piece of code executed. This action should only happen once, not on a daily basis or any other frequency. ...

Incorporate PHP functionality into an HTML document

I'm looking to incorporate a PHP code into my phoneGap application (HTML). I've attempted the following code, but unfortunately it's not functioning as expected. <html> <head> <title>PHP Integrated with HTML&l ...

Using the power of JavaScript to iterate through elements with the

Looking for a way to integrate JSON data into your simple bootstrap page? Here's a script that might help: Take a look at the following script: var xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState === 4 && ...

What steps do I need to take in order to generate a legitimate link annotation within Adobe Acrobat by utilizing Acrobat

Seeking guidance on how to embed an Acrobat Javascript within Adobe Acrobat in order to generate a link annotation. The current method involves using the "addLink" function within the document object, which triggers a Javascript action upon clicking the li ...

Managing two package.json files in a React project: best practices

After creating a React application in a folder using Node.js and npm, two sets of package.json and package-lock.json files were generated - one for the React app and another after installing react router dom. The additional set of files now exists above th ...

jQuery load() issue

$('form.comment_form').submit(function(e) { e.preventDefault(); var $form = $(this); $.ajax({ url : 'inc/process-form.php', type: 'POST', cache: true, data:({ comment ...

Modify HTML text using conditional CSS without JavaScript

Apologies for asking such a beginner question, but I've searched high and low for a similar post, to no avail. Here's my query: I have a paragraph text in my HTML code that I want to automatically replace with new text when I click a specific B ...

The statement ... is not a valid function, it has returned undefined

Currently experimenting with AngularJS, encountered an error message: Argument 'Controller' is not a function, got undefined View the JSFiddle link, along with HTML code: <h2>Hata's Tree-Like Set</h2> <div ng-app ng-init="N=3 ...

Tips for displaying the number of selected values in a select box label (e.g. Choose an Option)

I am currently using the jQuery multiselect API in my application to allow users to select multiple values. However, I am facing an issue where I need to fetch all the selected values when a button next to the multiselect box is clicked. Unfortunately, I h ...

Masking the identity of Google Pagespeed

Just starting to learn the ropes of javascript. How can I make Google Pagespeed anonymous? Check out the original code here: http://pastebin.com/xRbTekDA. It's functioning properly when I visit the page. Here is the anonymized version: http://pasteb ...

Animating pseudo-elements using Jquery

I'm having trouble animating the :after pseudo-element of my ul. Below is the CSS code I have: #slider .mypagination ul::after { content: ""; width:25%; bottom: 0; left:0; position: absolute; border-top:1px solid #7accc8; ...

The input tag loses focus after its value is updated using a class method in Angular 8.x

Currently, I am working on integrating a credit card payment method and formatting its number through specific methods. Here is how it is done: HTML <div class="form-group" *ngFor="let formField of cardFields; let cardFieldIndex = index;"> ...