Vert.x JsonArray

Vert.x JsonArray

In this tutorial, we are going to discuss about the Vert.x JsonArray. In the previous tutorial we have covered the Vert.x JsonObject. In this tutorial we will take a look at the Vert.x JsonArray object.

In Vert.x, a JsonArray is a data structure representing a JSON array. It stores an ordered sequence of values that can be accessed by index. A JsonArray can contain various types of data, including strings, numbers, booleans, JsonObjects, and even nested JsonArrays. It is often used in combination with JsonObject to represent more complex JSON structures.

Vert.x JsonArray
Creating a JsonArray

You can create a JsonArray in several ways, including adding values manually, parsing a JSON string, or converting a List into a JsonArray.

1. Create from Scratch

package com.ashok.vertx.vertx_starter.jsonarray;

import io.vertx.core.json.JsonArray;

/**
 *
 * @author ashok.mariyala
 *
 */
public class JsonArrayExample {
  public static void main(String[] args) {
    JsonArray jsonArray = new JsonArray();
    jsonArray.add("Ashok Kumar");
    jsonArray.add("Viansh");
    jsonArray.add(32);

    System.out.println(jsonArray.encodePrettily());

  }
}

Output

[ "Ashok Kumar", "Viansh", 32 ]

2. Create from a JSON String

You can parse a JSON string directly into a JsonArray.

package com.ashok.vertx.vertx_starter.jsonarray;

import io.vertx.core.json.JsonArray;

/**
 *
 * @author ashok.mariyala
 *
 */
public class JsonArrayExample {
  public static void main(String[] args) {
    String jsonString = "[\"Ashok Kumar\", \"Viansh\", 32]";
    JsonArray jsonArray = new JsonArray(jsonString);

    System.out.println(jsonArray.encodePrettily());
  }
}

Output

[ "Ashok Kumar", "Viansh", 32 ]

3. Create from a List

If you have a List in Java, you can convert it into a JsonArray.

package com.ashok.vertx.vertx_starter.jsonarray;

import io.vertx.core.json.JsonArray;

import java.util.Arrays;
import java.util.List;

/**
 *
 * @author ashok.mariyala
 *
 */
public class JsonArrayExample {
  public static void main(String[] args) {
    List<String> list = Arrays.asList("Ashok Kumar", "Viansh","Srirama");
    JsonArray jsonArray = new JsonArray(list);

    System.out.println(jsonArray.encodePrettily());
  }
}

Output

[ "Ashok Kumar", "Viansh", "Srirama" ]
Accessing Values in a JsonArray

Once a JsonArray is created, you can access the values using various get methods based on the data type of the value.

1. Accessing String Values

String name = jsonArray.getString(0);
System.out.println("First Name: " + name);

2. Accessing Integer Values

Integer age = jsonArray.getInteger(2);
System.out.println("Age: " + age);

3. Handling Nested JsonArray or JsonObject

JsonArray nestedArray = new JsonArray().add(1).add(2).add(3);
jsonArray.add(nestedArray);

JsonArray retrievedNestedArray = jsonArray.getJsonArray(3);
System.out.println("Nested Array: " + retrievedNestedArray.encodePrettily());
Encoding a JsonArray

You can encode a JsonArray into a JSON string using encode() or encodePrettily().

  • encode(): Produces a compact JSON string.
  • encodePrettily(): Produces a pretty-printed JSON string.
String compactJson = jsonArray.encode();
String prettyJson = jsonArray.encodePrettily();
Adding Different Data Types to JsonArray

A JsonArray can contain a mixture of data types, including JsonObject, strings, numbers, booleans, and more.

JsonObject user = new JsonObject().put("name", "Ashok Kumar").put("age", 32);
jsonArray.add(user);

jsonArray.add(true);  // Boolean
jsonArray.add(100.5); // Double
jsonArray.add("A simple string");

System.out.println(jsonArray.encodePrettily());

Output

[
  {
    "name": "Ashok Kumar",
    "age": 32
  },
  true,
  100.5,
  "A simple string"
]
Using JsonArray with the Vert.x Event Bus

Just like Vert.x JsonObject, you can send or receive Vert.x JsonArray over the Vert.x Event Bus. This is useful for passing collections of data between verticles or services.

Sending JsonArray on Event Bus

JsonArray arrayMessage = new JsonArray().add("Ashok Kumar").add("Viansh").add(32);
vertx.eventBus().send("array-address", arrayMessage);

Receiving JsonArray on Event Bus

vertx.eventBus().consumer("array-address", message -> {
    JsonArray receivedArray = (JsonArray) message.body();
    System.out.println("Received array: " + receivedArray.encodePrettily());
});
Merging Arrays

You can merge one Vert.x JsonArray into another.

JsonArray array1 = new JsonArray().add("Ashok Kumar").add("Viansh");
JsonArray array2 = new JsonArray().add("Srirama").add("Seetha");

array1.addAll(array2);
System.out.println(array1.encodePrettily());

Output

[
  "Ashok Kumar",
  "Vianh",
  "Srirama",
  "Seetha"
]
Summary
  • Vert.x JsonArray allows you to represent and manipulate JSON arrays with various data types.
  • You can create JsonArray from scratch, from JSON strings, or from Java List objects.
  • Vert.x JsonArray supports adding, updating, and removing elements, as well as nesting JsonObject and JsonArray within each other.
  • Vert.x JsonArray is commonly used in Vert.x’s event bus and HTTP APIs to handle structured data.

That’s all about the Vert.x JsonArray with examples. If you have any queries or feedback, please write us email at contact@waytoeasylearn.com. Enjoy learning, Enjoy Vert.x tutorials..!!

Vert.x JsonArray
Scroll to top