# A JSONArray text must start with '\[' Error

**URL:** <https://discourse.processing.org/t/a-jsonarray-text-must-start-with-error/15389>\
**Category:** Coding Questions\
**Created:** [November 11, 2019, 2:02am UTC](https://discourse.processing.org/t/a-jsonarray-text-must-start-with-error/15389 "2019-11-11T02:02:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![KeirMeDear](https://avatars.discourse-cdn.com/v4/letter/k/8c91f0/32.png) [@KeirMeDear](https://discourse.processing.org/u/KeirMeDear)\
**Post date:** [November 11, 2019, 2:02am UTC](https://discourse.processing.org/t/a-jsonarray-text-must-start-with-error/15389/1 "2019-11-11T02:02:02Z")

</div>

Hey,

So I’m trying to get to grips using JSON with processing and I’m having a couple of problems.

Currently I’ve created a piece of code which uses the Runescape API to loop through a list of known in game item id’s (stored as a txt file) and prints each of their names using the item ID through the Runescape API.

The problem is every so often I will randomly get the **A JSONObject text must begin with ‘{’’** exception. I thought it might be because some of the items use a JSONArray (and thus changed the code to account for this as you see bellow) but the same sort of exceptions still popped up regardless.

The thing that makes this even more confusing is that item ID’s might get checked once and be fine but then the next time they are checked they throw an exception. So item ID 2 might be fine the first time it’s checked but crash the code the second time it’s checked.

I’m not really sure where to go from here so any advice would be much appreciated,  
Many thanks in advance!

The Code:

```auto
int idNum = 0;

String[] idArray;

void setup() {
  size(500,500);
  idArray = loadStrings("allIds.txt");
}

void draw() {
  for (int i = 0; i < idArray.length; i++) {
    idNum = int(idArray[i]);
    try {
      JSONObject link = loadJSONObject("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
      JSONObject itemData = link.getJSONObject("item");
      String name = itemData.getString("name");
      println(idNum + name);
    } catch (Exception e) {
      JSONArray link = loadJSONArray("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
      for (int j = 0; j < link.size(); j++) {
        JSONObject values = link.getJSONObject(i); 
        String score = values.getString("name");
        println(score);
      }
    }
  }
delay(1000);
}

```

The object ids (stored as a txt file):

```auto
2
6
8
10
12
36
39
40
41
42
43
44
45
46
47
48
50
52
53
54
56
58
60
62
64
66
68
70
72
95
125
127
139
157
173
191
201
211
269
303
385
438
528
550
575
600
650
843
868
991
1031
1050
1149
1189
1291
1355
1379
1444

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [November 11, 2019, 10:56am UTC](https://discourse.processing.org/t/a-jsonarray-text-must-start-with-error/15389/2 "2019-11-11T10:56:23Z")

</div>

-a- the timer is one line to low

-b- you also not detect on ERROR 404

-c- you could start differently:  
but i not finished for you…

```auto
//https://discourse.processing.org/t/a-jsonarray-text-must-start-with-error/15389

int idNum = 0;

String[] idArray;

void setup() {
  size(500,500);
  idArray = loadStrings("allIds.txt");
}

void draw() {
  for (int i = 0; i < idArray.length; i++) {
    idNum = int(idArray[i]);
    String[] lines = loadStrings("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
    char first = trim(lines[0]).charAt(0);
    println( i , first );
    // you get the idea ? now change your following original code
    try {
      JSONObject link = loadJSONObject("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
      JSONObject itemData = link.getJSONObject("item");
      String name = itemData.getString("name");
      println(i+" "+idNum+" "+first+" "+ name);
    } catch (Exception e) {
      JSONArray link = loadJSONArray("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
      for (int j = 0; j < link.size(); j++) {
        JSONObject values = link.getJSONObject(i); 
        String score = values.getString("name");
        println(i+" "+idNum+" "+first+" "+ score);
      }
    }
  delay(2000);
  }
}

```

* * *

to all:

happy LOI KRATHONG

> **[Loi Krathong](https://en.wikipedia.org/wiki/Loi_Krathong)**
>
> Loi Krathong\[a\] (Thai: ลอยกระทง, pronounced \[lɔ̄ːj krā.tʰōŋ\]) is a Siamese festival celebrated annually throughout the Kingdom of Thailand and in nearby countries with significant southeastern Tai cultures (Laos, Shan, Mon, Tanintharyi, Kelantan, Kedah and Xishuangbanna). The name could be translated as "to float a basket," and comes from the tradition of making krathong or buoyant, decorated baskets, which are then floated on a river.
> Loi Krathong takes place on the evening of the full moon of...

 ![IMG_20191111_174908](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/e/e4e1a99b538a08a3d7a89931c6a2aa655836cc73.jpeg)

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [November 11, 2019, 9:25pm UTC](https://discourse.processing.org/t/a-jsonarray-text-must-start-with-error/15389/3 "2019-11-11T21:25:50Z")

</div>

in my tests, the page returns an empty array. ( `if (lines.length<=0)`)

This you have to skip (with `continue` command).

(maybe they return empty lines when too much inquiries from the same IP occur??)

No use to test for the first char in this case.

**TRIM**

But I used trim() on the String before parsing it which seemed to help

```auto
    lines[0] = trim(lines[0]);
    JSONObject link = parseJSONObject(lines[0]);

```

**Result**

here is one result:

(but the results were different every time)  
(one star is one empty line)

```auto
 0 2 { Cannonball
 1 6 { Cannon base
 2 8 { Cannon stand
 3 10 { Cannon barrels
 4 12 { Cannon furnace
 5 36 { Candle
 6 39 { Bronze arrowheads
 7 40 { Iron arrowheads
 8 41 { Steel arrowheads
 9 42 { Mithril arrowheads
 10 43 { Adamant arrowheads
 11 44 { Rune arrowheads
 12 45 { Opal bolt tips
 13 46 { Pearl bolt tips
 14 47 { Barb bolttips
 ***************** 33 157 { Super strength (3)
 **** 38 269 { Clean torstol
 ************ 51 1050 { Santa hat
 52 1149 { Dragon helm
 *****

```

Chrisir

* * *

**Code**

```auto
int idNum = 0;

int[] idArray={
  2, 
  6, 
  8, 
  10, 
  12, 
  36, 
  39, 
  40, 
  41, 
  42, 
  43, 
  44, 
  45, 
  46, 
  47, 
  48, 
  50, 
  52, 
  53, 
  54, 
  56, 
  58, 
  60, 
  62, 
  64, 
  66, 
  68, 
  70, 
  72, 
  95, 
  125, 
  127, 
  139, 
  157, 
  173, 
  191, 
  201, 
  211, 
  269, 
  303, 
  385, 
  438, 
  528, 
  550, 
  575, 
  600, 
  650, 
  843, 
  868, 
  991, 
  1031, 
  1050, 
  1149, 
  1189, 
  1291, 
  1355, 
  1379, 
  1444
};

// --------------------------------------------------------------------------------------------------------------------

void setup() {
  size(500, 500);
  noLoop();
  // idArray = loadStrings("allIds.txt");
}

void draw() {
  for (int i = 0; i < idArray.length; i++) {

    idNum = int(idArray[i]);

    String[] lines = null;

    lines=loadStrings("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);

    if (lines.length<=0) { // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
      print("*");
      //printArray(lines);
      continue; // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    }

    // println("test 1");
    // printArray(lines);
    char first = trim(lines[0]).charAt(0);
    // println( i, first );

    // you get the idea ? now change your following original code

    //if (first=='{') 
    // println("OK");
    lines[0] = trim(lines[0]);
    JSONObject link = parseJSONObject(lines[0]);
    // --------------------------------------------
    // JSONObject link = loadJSONObject("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
    JSONObject itemData = link.getJSONObject("item");
    String name = itemData.getString("name");
    println(i+" "+idNum+" "+first+" "+ name);

    /*
    catch (Exception e) {
     JSONArray link = loadJSONArray("http://services.runescape.com/m=itemdb_rs/api/catalogue/detail.json?item=" + idNum);
     for (int j = 0; j < link.size(); j++) {
     JSONObject values = link.getJSONObject(i); 
     String score = values.getString("name");
     println(i+" "+idNum+" "+first+" "+ score);
     }
     }
     */
    delay(2110);
  }//for

  // end message 
  println("");
  println("-----------------------------------------------------------------------------------------");
}
//

```
