Having troubles parsing JSON data


I am trying to parse some json data for the first time in processing, but I meet with fail.

I want to process data fetched from arduino-cli.exe to determen to which com port an arduino is connected and what it’s board type is.

Arduino-cli parsed the data as json. It poops out an array of JSON objects as far as I understand.

        Process p = launch(command);
        BufferedReader in = new BufferedReader(new InputStreamReader( p.getInputStream()));
        while ((line = in.readLine(  )) != null) { jsonData += line ;  } // store all data in this string
        JSONArray jsonArray = parseJSONArray(jsonData); // parse the array into separate objects.
        if (jsonArray == null)
            println("JSONArray could not be parsed");
            println("printing com port things") ;
            JSONObject port = jsonArray.getJSONObject(0); 

            println("printing json array element") ;
            println(port) ;               // so far so good, see output below for result.

            println("printing port") ;
            COM_PORT = port.getString("address"); // here I am trying to fetch the comport number, and this is not working.
            println( COM_PORT ) ;

            println("printing boardName");
            boardName = port.getString("matching_boards");
            println( boardName ) ;

The above code snippet yields this output.

  "port": {
    "protocol": "serial",
    "address": "COM4",
    "protocol_label": "Serial Port (USB)",
    "label": "COM4",
    "properties": {
      "vid": "0x2341",
      "serialNumber": "85735313333351612150",
      "pid": "0x0043"
    "hardware_id": "85735313333351612150"
  "matching_boards": [{
    "fqbn": "arduino:avr:uno",
    "name": "Arduino Uno"

What am I doing wrong? and how should I do this?

Kind regards,


Thankfully I have a coworker here who knows a little more about json.

The most usefull trick is method chaining. I had an json array in an other array.

This stretch code solved the issue

            for (int i = 0; i < mainArray.size(); i++)
                JSONObject json = mainArray.getJSONObject(i); 
                COM_PORT = json.getJSONObject("port").getString("address");
                if( json.getJSONArray("matching_boards") != null )
                    JSONObject subObj = json.getJSONArray("matching_boards").getJSONObject(0);
                    fqbn = subObj.getString("fqbn") ;
                    break ;

It was slightly more difficult than I hoped :expressionless:

My entire program does now work well. I can succesfully retreive the fqbn string and corresponding com port number with which I can upload programs. In theory it should not matter, what board you plug in (as long as it is an avr due to installed cores) and which com port number your computer assigns to the arduino.