Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

153
Visualizações
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:

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

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.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda