Example of creating JSON Array in JAVA

To know more about JSON , read “What is JSON and Understanding JSON syntax with simple example” and “Cloud Technologies”

In this post, we will show you how to write a simple JSON Array by writing code in JAVA. This post is related to our previous post “Example of creating simple JSON in JAVA”

$ vim simpleJsonArrayExample.java
import org.json.simple.JSONObject;
import org.json.simple.JSONArray;

public class simpleJsonArrayExample {
        public static void main(String args[]) {
                JSONObject myObject = new JSONObject();
                myObject.put("myname", "lynxbee");
                myObject.put("myurl", "www.lynxbee.com");

                JSONArray myArray = new JSONArray();
                JSONObject firstObjectInsideArray = new JSONObject();
                firstObjectInsideArray.put("cms", "wordpress");
                myArray.add(firstObjectInsideArray);
                JSONObject secondObjectInsideArray = new JSONObject();
                secondObjectInsideArray.put("platform", "linux");
                myArray.add(secondObjectInsideArray);

                myObject.put("technology", myArray);

                System.out.println("We created JSON Array as example : " + myObject);
        }
}

Lets try to understand few things, the JSON array has been created by using following code,

JSONArray myArray = new JSONArray();

and JSON objects which we add to array are created using following code,

JSONObject firstObjectInsideArray = new JSONObject();
firstObjectInsideArray.put("cms", "wordpress");
JSONObject secondObjectInsideArray = new JSONObject();
secondObjectInsideArray.put("platform", "linux");

Here, we tried to created two JSON objects which we will add to array as,

myArray.add(firstObjectInsideArray);
myArray.add(secondObjectInsideArray);

Now, Compile this program using javac compiler in Ubuntu as,

$ javac simpleJsonArrayExample.java

Error we received when tried to compile this program on Ubuntu,

javac simpleJsonArrayExample.java 
simpleJsonArrayExample.java:1: error: package org.json.simple does not exist
import org.json.simple.JSONObject;

this error means, there is no JSON package is installed on your Ubuntu machine, so lets try and resolve this error…

$ sudo apt-get install libjson-simple-java

you can see this JSON library is installed as,

$ ls -alh /usr/share/java/json*
/usr/share/java/json-simple-2.3.0.jar -> json-simple.jar
/usr/share/java/json-simple.jar

Now we need to add this library to classpath as,

$ export CLASSPATH=$CLASSPATH:/usr/share/java/json-simple.jar
$ echo $CLASSPATH
:/usr/share/java/json-simple.jar

Now, lets again compile this program as,

$ javac simpleJsonArrayExample.java

Now, if we run the compiled JAVA program, we can see that we created simple JSON with only one object as below,

$ java simpleJsonArrayExample
We created JSON Array as example : {"myname":"lynxbee","myurl":"www.lynxbee.com","technology":[{"cms":"wordpress"},{"platform":"linux"}]}

As you can see, when we properly lint the JSON we can clearly see we are able to create an array of JSON objects i.e. “technology” is an array of objects…

{
	"myname": "lynxbee",
	"myurl": "www.lynxbee.com",
	"technology": [{
		"cms": "wordpress"
	}, {
		"platform": "linux"
	}]
}

Leave a Comment