Unable to get data from .JSON files

#1

Hi All,

I have been able to get data from JSON files before, but i am unable to get data from this one ‘https://www.osrsbox.com/osrsbox-db/items-json-slot/items-2h.json

function preload(){
 items = loadJSON('https://www.osrsbox.com/osrsbox-db/items-json-slot/items-2h.json')
  
}


function setup() {
  noCanvas();
  print(items);
  Item = items.839.id
  
  print(Item.length);
  for (var i = 0; i < Item.length; i++){
    print(Item[i]);
    
  }
}

function draw() {

}

I am getting Error on Item = items.839.id with this error message ‘Uncaught SyntaxError: Unexpected number (sketch: line 10)’

Is anyone able to help? thanks in advance!

Thanks

Ryan

1 Like

#2




void  setup() {

  JSONObject  items = loadJSONObject("https://www.osrsbox.com/osrsbox-db/items-json-slot/items-2h.json");

  //  println(items); 

  JSONObject  Item = items.getJSONObject("839"); 

  println(Item.size());
  println("-----------------------"); 
  for (int i = 0; i < Item.size(); i++) {
    print(Item.get(str(i))+" ");
  }
}

void  draw() {
  //
}
2 Likes

#3

Hi Chrisir, thanks for reply! Is there a way of looping through all the JSON objects in the JSON file?

0 Likes

#4

they all seem to be null


void  setup() {

  JSONObject  items = loadJSONObject("https://www.osrsbox.com/osrsbox-db/items-json-slot/items-2h.json");

  //  println(items); 

  for (int i = 0; i < items.size(); i++) {

    JSONObject  Item = items.getJSONObject(str(i)); 
    println (i);
    println(Item); 
    println ("------------------------------------------------------------------");
    // print(" "+Item.get(str(0))+" ");
  }

  //println(Item.size());
  //println("-----------------------"); 
  //for (int i = 0; i < Item.size(); i++) {
  //  print(Item.get(str(i))+" ");
  //}
}

void  draw() {
  //
}
1 Like

#5

Some online sketch examples on JSON: :sunglasses:



1 Like

#6

HI,

I cant seem to get JSONObject to work in the P5 online editor? Am I missing something here?

Thanks

Ryan

0 Likes

#7

First, notice that this there is no such syntax items.839.id.

Second, Chrisir gave you a Processing(Java) solution – for p5.js, JSON is imported directly to native JavaScript objects, which you then access with normal JavaScript notation. Additional reference / examples:

https://p5js.org/reference/#/p5/loadJSON
https://p5js.org/examples/advanced-data-load-saved-json.html

3 Likes