Accessing json data from wunderground API? - javascript

I have this async function below that taps into a weather API and I would just like to retrieve two pieces of info from the API: Temperature in F & C. I removed the API_Key but it's free to get one on the site if necessary.
I can confirm I am receiving the json object due to my console.log(response) statement, but Im not sure how to access these data points in such heavily embedded json notation.
I guess my question is if I wanted to access say 'full' for full city name I thought I'd do something like response.observation_location.full but that doesn't work...
Help?
async loadWeather() {
// let zip = this.args.zip || 97239;
let response = await fetch(`http://api.wunderground.com/api/API_KEY/conditions/q/CA/San_Francisco.json`);
console.log(response);
this.weather = await response.json();
// setTimeout( () => { this.loadWeather(); }, 2000);
}
Here is a partial output of the response json:
{
"response": {
"version": "0.1",
"termsofService": "http://www.wunderground.com/weather/api/d/terms.html",
"features": {
"conditions": 1
}
},
"current_observation": {
"image": {
"url": "http://icons.wxug.com/graphics/wu2/logo_130x80.png",
"title": "Weather Underground",
"link": "http://www.wunderground.com"
},
"display_location": {
"full": "San Francisco, CA",
"city": "San Francisco",
"state": "CA",
"state_name": "California",
"country": "US",
"country_iso3166": "US",
"zip": "94102",
"magic": "1",
"wmo": "99999",
"latitude": "37.77999878",
"longitude": "-122.41999817",
"elevation": "60.0"
},
"observation_location": {
"full": "SOMA, San Francisco, California",
"city": "SOMA, San Francisco",
"state": "California",
"country": "US",
"country_iso3166": "US",
"latitude": "37.778488",
"longitude": "-122.408005",
"elevation": "23 ft"
},
I have tried doing console.log(response["current_observation"]) just to access a nested data value but that doesnt seem to work as it returns undefined.

Ok I solved my own issue, but for the record:
response needed to be converted to json via resonse.json()
and then I can access properties as expected
this.weather = await response.json();
console.log(this.weather["current_observation"]["temp_f"]);
console.log(this.weather["current_observation"]["temp_c"]);

Related

Mongoose - find documents that match one or more conditions

I have three documents that look like this
{
"name": "X1",
"location": {
"country": "Ireland",
"state": "Dublin",
},
},
{
"name": "X2",
"location": {
"country": "Ireland",
"state": "Dublin",
"address": ""
},
},
{
"name": "C3",
"location": {
"country": "United States of America",
"state": "California",
"address": "San Jose"
},
I am trying to implement a filtering functionality to show documents based on multiple locations.
so, the endpoint URL looks like this for example http://localhost:3000/api/companies/stacks?state[]=Dublin&country[]=United%20States%20of%20America
So, it should return the three documents that are shown above, yet it only returns this document
{
"name": "C3",
"location": {
"country": "United States of America",
"state": "California",
"address": "San Jose"
},
Here is the source code, I am using Mongoose mainly.
//Filtering by location
if ('country' in req.query) {
const countries = [...country]
conditions['location.country']={$in:countries}
}
if ('state' in req.query) {
const states = [...state]
conditions['location.states'] = { $in: states }
}
const doc = await model
.find(conditions)
.sort({ p: -1, _id: 1 })
.lean()
.exec()
I understood that it returns the document that matches the country parameter, but I don't understand why it doesn't also fetch the other documents that has Dublin in states?
Edit: I also tried to use $or operator like that
find({
$or:[
{'location.country':'United States of America'},
{'location.states':'Dublin'}
]
})
Yet it also, returned the document that matches the country only. I thought about using aggregates but I am not sure if it is going to help me in my case?
Thanks in advance.

Verify String is displaying within Nested JSON using Postman

I am working on Postman to verify some API calls, upon which I have gone through one of the end point, whose response is give below, and I need to make sure that within that JSON response:
[
{
"contact": {
"id": "k72yk2iwrf",
"firstName": "Francis",
"lastName": "Abell",
"title": "Translational Science Project Manager",
"company": "Sensei",
"email": "aa#aa.cpom",
"fax": {},
"businessAddress": {
"line1": "road",
"line2": "Street",
"line3": "Suite 710",
"city": "Boston",
"country": "US",
"postalCode": "02210",
"state": "MA"
},
"businessPhone": {
"number": "123-123-1234",
"ext": ""
},
"homeAddress": {},
"homePhone": {},
"mobilePhone": {}
},
"registration": {
"id": "104656",
"badgeId": "9208113975",
"eventId": "TESTLIBRA-10"
}
},
{
"contact": {
"id": "w4c4f2i7l4",
"firstName": "Francis",
"lastName": "Abell",
"title": "Translational Science Project Manager",
"company": "Sensei",
"email": "aa#aa.cpom",
"fax": {},
"businessAddress": {
"line1": "road",
"line2": "Street",
"line3": "Suite 710",
"city": "Boston",
"country": "US",
"postalCode": "02210",
"state": "MA"
},
"businessPhone": {
"number": "123-123-1234",
"ext": ""
},
"homeAddress": {},
"homePhone": {},
"mobilePhone": {}
},
"registration": {
"id": "104656",
"badgeId": "6803424516",
"eventId": "TESTLIBRA-10"
}
}
]
I can make sure that "eventId" is displaying and it is displaying "TESTLIBRA-10" value.
No matter, how long JSON response is, It can verify that this property , along with that value of that property are displaying.
I got my answer by myself, what I did was:
var jsonArrayData = pm.response.json();
pm.test('EventID property is displaying throughout JSON', function(){
jsonArrayData.each(function(eventID){
pm.expect(eventID.registration).to.have.property("eventId")
})
})
pm.test('Entered Libra EventID is entered', function(){
jsonArrayData.each(function(eventID){
pm.expect(eventID.registration.eventId).to.eql("TESTLIBRA-10")
})
})

Find all cities in a country/state or finding the state/country given a city from JSON data

I have county/state/city data in JSON (shown below). I want to find all cities given a state/country and given a city, need to find the state and country.
List all cities in country: usa
List all cities in states: Alabama
List the state and country for city: Adamsville
{
"country": "usa",
"states": [
{
"name": "Alabama",
"state_code": "AL",
"cities": [
{
"name": "Abbeville",
"latitude": "31.57184000",
"longitude": "-85.25049000"
},
{
"name": "Adamsville",
"latitude": "33.60094000",
"longitude": "-86.95611000"
}
]
},
{
"name": "Alaska",
"state_code": "AK",
"cities": [
{
"name": "Akutan",
"latitude": "54.13350000",
"longitude": "-165.77686000"
},
{
"name": "Aleutians East Borough",
"latitude": "54.85000000",
"longitude": "-163.41667000"
}
]
}
]
}
Here are built-in Array functions that will help you get what you want.
Array.prototype.find() - returns the value of the first element in an array that satisfies the provided test.
To find the state object in your JSON data, you can do this:
const findState = (name) => data.states.find(state => state.name === name);
let alaska = findState('Alaska');
// { "name"; "Alaska", "state_code": "AK", "cities": [...] }
To find the cities in a state, use the above findState to get the state object, then Array.prototype.map() will form an array of just the city names:
let alaska = findState('Alaska');
let citiesInAlaska = alaska.cities.map(city => city.name);
// [ "Akutan", "Aleutians East Borough" ]

How to find the closest IP from a list of IPs?

I want to know if that's possible
I have one given IP, example: 191.184.222.43 (public ipv4)
And I have a Loong list of other IP addresses, example:
[ ip1, ip2, ip3, ip4 ... and so on ]
Now I have to find which IP of my list i'ts closest (physically) to my given IP (191.184.222.43)
Is it that possible? Or am I thinking differently
There are many API's that can accomplish this one example of one is over at https://ipstack.com/ . It geolocates based on IP and with some parsing you can easily accomplish this!
Example Code:
// set endpoint and your access key
var ip = '134.201.250.155'
var access_key = 'YOUR_ACCESS_KEY';
// get the API result via jQuery.ajax
$.ajax({
url: 'https://api.ipstack.com/' + ip + '?access_key=' + access_key,
dataType: 'jsonp',
success: function(json) {
// output the "capital" object inside "location"
alert(json.location.capital);
}
});
Example Output (JSON):
[
{
"ip": "134.201.250.155",
"type": "ipv4",
"continent_code": "NA",
"continent_name": "North America",
"country_code": "US",
"country_name": "United States",
"region_code": "CA",
"region_name": "California",
"city": "Los Angeles",
"zip": "90013",
"latitude": 34.0453,
"longitude": -118.2413,
"location": { ... },
"time_zone": { ... },
"currency": { ... },
"connection": { ... },
}
]

angularjs response data does not show in console

i have below code i but data not show in console log please help me i am new in angular.. AND how to show data in HTML
this.$http.get(properties.client+'/123')
.then(response => {
$scope.clientdata= response.data;
console.log($scope.clientdata.address);
});
address array dose not show in console log,i don't know why RESPONSE BELOW
[{ "_id": "123",
"__v": 1,
"history": [],
"address": {
"street_address": "adsf",
"state": "adsf",
"zip_code": "adsf",
"phone_number": "asdf",
"country": "ads",
"city": "asdf",
"site_name": "adsf",
"_id": "123123",
"geolocation": {
"coordinates": [],
"type": "Point"
},
"location": {
"latitude": null,
"longitude": null
},
"id": "5835baaa71a30ca8319b6e36"
},
"current_status": "Assigned",
"time": 0
}]
Your clientdata is an array as per the post, try
console.log($scope.clientdata[0].address);
HTML
{{clientdata[0].address}}
From the structure of your JSON data, clientdata is an array of objects and as of current, there is only one object in the array. So, in order to access the address property of the object, you will have to access it in this way
$scope.clientdata[0].address
and in HTML
{{clientdata[0].address._id}}
and rest of the address object properties in similar fashion.

Categories

Resources