Problem attempting to create a large 2D array from a JSON

edited June 2017 in Questions about Code

I'm working on a personal project that will render a low-quality (32x32) heightmap as 3D terrain. I'm having issues loading the terrain file, which I've defined as a JSON file with a particular format, as a big 2D array. I'm probably doing many things horribly wrong (especially loading array elements from a JSON), please help me...

JSON object format:

  "row": 0,
  "data": [ -128, -109, -70, ... 30, 102 ]

There's 32 of them labeled 0 to 31, and each "data" array has 32 values ranging from -128 to 127. I've verified that the JSON is valid.


JSONArray rawHeight;
byte[][] hmap = null;
byte[] temp = null;

void prepareHMapAndTemp() {
  for (int h = 0; h < 32; h++) {
    hmap[i] = new byte[32];
    temp[i] = new byte;

void processHeightData() {
  for (int i = 0; i < 32; i++) {
    JSONObject row = rawHeight.getJSONObject(i);

    for (int j = 0; j < 32; j++) {
      temp = (byte[])append(hmap, row.getJSONObject("data").getInt(j));
    hmap = (byte[][])append(hmap, temp);

    println(i + ": " + row);

void setup() {
  rawHeight = loadJSONArray("map.hmp");

On temp[i] = new byte;, byte; is underlined and gives this very ambiguous error: Error on "Dimensions". Trying to run it anyways has it yelling unexpected token ";". Also, temp = (byte[])append(hmap, row.getJSONObject("data").getInt(j)); gives the error The function "getInt()" expects parameters like: "getInt(String)" even though the online reference says it takes an int for the index?

Edit: Just realized I was I started hmap without a value and temp with a value of null. Now both are be started as null. Don't think there's actually a difference, though.


  • Try:

    final int N=32;
    byte[][] hmap = null;
    byte[] temp = null;
    void prepareHMapAndTemp() {
      hmap = new byte[N][];
      temp = new byte[N];
      for (int h = 0; h < N; h++) {
        hmap[h] = new byte[N];
        for (int j = 0; j < N; j++) {
          hmap[h][j] =0;
        temp[h] = 0;

    For line 17 and 19, you should be doing something like:

    temp[j] = row.getJSONObject("data").getInt(j);

    Can you share your file, or you can copy and paste its content here or send it to me by PM.


  • edited June 2017

    I'm about to try that. And that was the entire file, minus a couple comments I put in for myself. Working on getting the heightmap loaded before anything else.

    Edit: tried it, and with some mucking around I got it to not give me red squiggly lines in the IDE. It still gives me a NullPointerException when trying to load each int from the "data" objects in the JSON.

  • The data in your first comment is all the data you have right now? Then you will get a NPE because the code is try to load an array of 32 by 32. Either use a data set with 32 x 32 data array as input or modify the code to read only available data.


  • He means the data file

  • edited June 2017

    No, I do have an entire file with 32 objects that have 32 points each. I meant that was all the code (but I've PM'd you the new code)

  • Can you share your file, or you can copy and paste its content here or send it to me by PM.

    Sorry, I was not clear, I was inquiring about your data file.


  • Oh, yeah. It's a lot of text so here's a pastebin.

  • Answer ✓

    Here is a tested version.


    final int arraySize = 32; // size of array
    final int STEP=10;
    JSONArray rawHeight; // JSON array, will directly loaded from the file
    byte[][] hmap = null; // will become the processed heightmap
    byte[] temp = null; // will be filled up with each "data" array and appended to hmap
    // performs 1-time setup
    public void setup() {
      size(320, 320);  
    public void draw() {
      for (int i = 0; i < arraySize; i++) {
        for (int j = 0; j < arraySize; j++) {
          rect(i*10, j*10, 10, 10);
    // prepares the hmap and temp arrays to avoid NullPointerException's wrath
    public void prepareHMapAndTemp() {
      hmap = new byte[arraySize][];
      temp = new byte[arraySize];
      for (int h = 0; h < arraySize; h++) {
        //hmap[h] = new byte[arraySize];             //REMARK: Not needed bc of "hmap[j]=byte(values);"
        //for (int i = 0; i < arraySize; i++) {
        //  hmap[h][i] = 0;
        temp[h] = 0;
    // processes heightmap JSON and stores it as the hmap[][] array
    public void processHeightData() {
      rawHeight = loadJSONArray("data.json"); // load JSON array named map.hmp in the project's data directory   
      for (int j = 0; j < rawHeight.size(); j++) {
        JSONObject row = rawHeight.getJSONObject(j);    
        //println(row.toString());   //DEBUG line
        JSONArray row2 = row.getJSONArray("data");    
        int[] values = row2.getIntArray();
        hmap[j]=byte(values);    //Loading heat map here!
        //DEBUG block
        print("JSON entry="+j);
        print(" SIZE="+row.size());  
        println(" VALS="+values.length);
        for (int k = 0; k < values.length; k++) {
          print(" "+values[k]);
  • Yep, that worked. Thanks!

Sign In or Register to comment.