Reloading a JSON in p5

Hello. I’m currently working with geocoding API with Mapquest. I have this code written in setup right now.

function setup(){
  canvas = createCanvas(840,840); 
  myMap = mappa.tileMap(options); 
  myMap.overlay(canvas);
  myMap.onChange(drawPoint); 
  myMap.onChange(drawLine);
  url = lapi+lApiKey+format+county;
  ljson = loadJSON(url); //this starts as an empty object
  noLoop();
}

The next step I want to take is to save this JSON data into a variable so it continues to display on the map. Then I want to update the url to have it look for a new county. 
I do have this code completed here: 

function mouseClicked() {
  // print(ljson);
  county = mjson[13][5];
  county = county.replace(' ', '+');
  county = county.split(',').join('+');
  county = county.replace(' ', '');
   url = lapi+lApiKey+format+county;
  ljson = loadJSON(url);
//setup();
}

However, what happens here is that ljson becomes an empty object. I have tried calling setup() again to no effect. My goal is that once that is updated, then I want to reload this new JSON. I know I could do this with multiple variables (having many separate urls) but I was wondering if there was a way to reload the JSON within the code.

1 Like

you might read again
https://p5js.org/reference/#/p5/loadJSON
there is a second example with callback function.
and try my test:
https://editor.p5js.org/kll/sketches/QpZ6AtD4a

1 Like

Your example helped a lot! I am able to load my two Jsons smoothly now.

1 Like