• Jobs
  • About Us
  • professionals
    • Home
    • Jobs
    • Courses and challenges
    • Questions
    • Teachers
  • business
    • Home
    • Post vacancy
    • Our process
    • Pricing
    • Assessments
    • Payroll
    • Blog
    • Sales
    • Salary Calculator

0

108
Views
Check for a specific JSON result and stop if not present

First thing first, I am new to Javascript and coding so if my code isn't as tidy or succinct as it could be then please feel free to suggest ways to tidy it up.

I have seen a few similar questions to this topic but nothing that does what I specifically need.

I have a JS code that works nicely that takes a JSON input and returns a specific value:

var subtitle = document.getElementById("demo");
var title = document.getElementById("title_location");
var para1 = document.getElementById("para1");

navigator.geolocation.getCurrentPosition(showPosition);

function showPosition(position) {

  var url = "https://api.opencagedata.com/geocode/v1/json?q=52.773322+-1.552661&key=592431a9da4a45b686bc75eafb005cc1" //Swadlincote (already a city so doesnt need replacing)


  fetch(url)
    .then(response => response.json())
    .then(data => {
      console.log(data)
      console.log(data.results[0].components)


      var stringified = JSON.stringify(data);
      console.log("this is the stringified json before replacement: " + stringified)
      stringifiedR = stringified.replace('"town"', '"city"');
      console.log("this is the stringified json after replacement: " + stringifiedR)
      var jsonObject = JSON.parse(stringifiedR);
      console.log(jsonObject)
      console.log("stringified city: : " + jsonObject.results[0].components.city)

      subtitle.innerHTML = "Subtitle goes here: " + jsonObject.results[0].components.city
      title.innerHTML = "Page Title Goes Here: " + jsonObject.results[0].components.city
      para1.innerHTML = "1st paragraph of text goes here: " + jsonObject.results[0].components.city
    })

}

This works nicely and returns the value from data.results[0].components.city which is what I need.

What I am looking for is a way to check if the 'country' tag in data.results[0].components.country matches "United Kingdom" before I do anything with the script and if the data matches, proceed with the rest of the script, if the 'country' tag matches anything other than 'United Kingdom' then stop the script.

Does anybody have any ideas they would be happy to share.

Thanks in advance:

almost 3 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It looks like you just need to do a comparison:

if (jsonObject.results[0].components.country === 'United Kingdom') { 
    console.log('is United Kingdom')
    // rest of what you want to do
} else {
    console.log('is not United Kingdom')
}

You may also want to build in some flexibility (such as making the comparison no case sensitive in case the data returned may not be cleaning inputted).

More info on comparisons: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness

I would also recommend not publicly posting your api keys (especially if you have to pay to use an api or are throttled for excessive use). It would be better to post some sample JSON in this case.

almost 3 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Our process Sales
Legal
Terms and conditions Privacy policy
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recommend me some offers
I have an error