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

Dealing with challenges in Jest mocking technology

I'm having trouble mocking the async function declared inside a class using jest.js in node.js. When I attempt to mock the function, it seems to bypass the mock and execute the actual implementation. Strangely, another user was able to successfully mo ...

Real-time File Updates Display with Node.js and Express.js

Seeking guidance on whether there is a Node module available to utilize the Express module for displaying real-time updates in the browser using data from a file. I have a regularly updated .csv file and I am looking to showcase these updates instantly on ...

Encountered a NodeJS error when attempting to access the localhost server

Hi everyone, I hope you are all doing well. I am in the process of converting this application to Docker but encountering a "Cannot GET / NodeJS" error. Below is a snippet from my express.js file: const express = require("express"); const app = express(); ...

Utilize Knex to retrieve data from the req.query

express and knex have been giving me some trouble; I am struggling to get this endpoint working using req.querys (response from express), even though it worked fine when I used req.params. Express: app.get(`/actor`, async (req: Request, res: Response) =&g ...

Having difficulty utilizing the express.session module in conjunction with HTTPS

I need to implement authentication and session creation on a HTTPS static website using expressjs. Here is the code snippet: app.js: // Set up the https server var express = require('express'); var https = require('https'); var http ...

Looking for a way to add a CSS effect that reveals another image or text when hovering over an image?

My company's website features 10 country flags that, when clicked, display the entire site in that country's language for the user's session. However, I want to take it a step further and have a small image of the respective country appear w ...

What are the recommended guidelines for using TypeScript effectively?

When facing difficulties, I have an array with functions, such as: this._array = [handler, func, type] How should I declare this private property? 1. Array<any> 2. any[] 3. T[] 4. Array<T> What is the difference in these declarations? ...

Creating a Flot Bar Chart that displays non-stacking values

I am new to using Flot for creating charts. Currently, I have a bar chart displayed below: https://i.stack.imgur.com/RSumf.png Here is the code snippet I utilized to generate this chart: $.getJSON('chartBar.json', function(graphDataBar){ $ ...

What could be causing this code to only modify one element instead of both?

One input field should be able to change two elements, however with jQuery it appears that only one element is being modified. When rearranging the order of elements in the function, such as having #inCoin first and then #receive_amount, only the #inCoin ...

Save information on users' Google accounts

Utilizing this resource, I successfully implemented a login feature on my website. The implementation includes functions such as onSignIn, signOut, and auth2.isSignedIn.get() to manage user authentication. Now, I am looking for a way to extract specific d ...

Performing an axios request using form data in a React JS application

I am trying to figure out how to use axios in react js to make a cURL request that is currently working. Here is the cURL request: curl -k --request GET "BASE_URL_SERVER/sendText" --form "user_id='uidxxxx'" --form "sign_id=" Every time I try to ...

Trouble updating document with MongoDB updateOne when using ID as filter

I need to update a property value of a specific document by sending a request to my NextJs API using fetch. // Update items in state when the pending time in queue has passed, set allowed: true items.map((item) => { const itemDate = new Date(item.adde ...

Extract the label from Chip component within the onClick() event in material-ui

-- Using Material-UI with React and Redux -- Within my material-ui table, there are <TableRow> elements each containing multiple <TableCell> components with <Chip> elements. These <Chip> components display text via their label prop ...

Display HTML content generated by JavaScript in the page source

Can the HTML elements I've added through JavaScript and jQuery codes be displayed in the page source (ctrl+U)? ...

Submit simple text via XMLHttpRequest to an Express server

I am currently facing an issue with making a post XMLHttpRequest to my Express server. Despite attempting to send a string, it seems that I am missing something crucial in my implementation. Client: const sendMessage = () => { const message = "This ...

Some elements are not visible when inspecting the source code

Hidden fields are essential in my jsp form. Interestingly, when I check the page source in a browser, only the first hidden field is displayed, not the second one. <input type="hidden" name="url" id="url" value="<%=url%>" /> <input type="hi ...

Tips on creating a literal type that is determined by a specific value within an object

In the flow, I am able to create a dynamic literal type in this manner: const myVar = 'foo' type X = { [typeof myVar]: string } const myX: X = { foo: 1 } // will throw, because number const myX: X = { foo: 'bar' } // will not throw ...

Is it necessary to keep the package-lock.json file versioned in git repository?

With the arrival of npm 5 and nodejs 8, a new file called package-lock.json has been introduced. I'm curious to know if this file should be included in version control or ignored in git. ...

Tips for choosing a drop-down menu option without an identifier using Selenium in Python

I am trying to choose an element/item from a drop-down menu in Python and Selenium without using an id element. Here is the HTML code snippet: <mbo-transaction-mode-select mode="filters.mode" class="ng-isolate-scope"> <select class="form-con ...

Guide to inspecting file contents with Node.js

I am working on viewing the content of a file that is posted from the client using the fs module. However, with the code below, the contents are coming up as undefined. Can anyone help me identify what is missing in the code? To ensure I am receiving the ...