2

I'd like to split a JsonArray from the package javax.json and I can't find a convenient method that does the job. I looked through the documentation and I can only think of iterating over the JsonArray and adding the items manually using the JsonArrayBuilder.

Is there a more convenient way to split the array?

Please note that I can't use another package (such as google.gson).

Thanks in advance.

edit: I have a JsonArray of size N and I'd like to split it by indexes from and to and save the result as a JsonArray.

something like this:

JsonArray array1 = getJsonArrayOfSize(10);
JsonArray array2 = array1.split(2,5);
4
  • Convert your JsonArray to List and then split each items in List. Commented Dec 30, 2019 at 9:56
  • Show an example? Commented Dec 30, 2019 at 9:57
  • Can you elaborate what you mean by split the array? Commented Dec 30, 2019 at 9:58
  • @VishwaRatna I added an example. Commented Dec 30, 2019 at 10:32

2 Answers 2

4

You can use subList method:

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonArrayBuilder;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public class JsonApp {

    public static void main(String[] args) {
        List<Integer> ints = IntStream.range(1, 10).boxed().collect(Collectors.toList());
        JsonArrayExt array = new JsonArrayExt(ints);

        JsonArray array1 = array.subList(2, 5);
        System.out.println(array);
        System.out.println(array1);
    }
}

class JsonArrayExt {
    private final JsonArray array;

    public JsonArrayExt(List<Integer> ints) {
        this(Json.createArrayBuilder(ints).build());
    }

    public JsonArrayExt(JsonArray array) {
        this.array = Objects.requireNonNull(array);
    }

    public JsonArray subList(int fromIndex, int toIndex) {
        JsonArrayBuilder builder = Json.createArrayBuilder();
        array.subList(fromIndex, toIndex).forEach(builder::add);

        return builder.build();
    }

    @Override
    public String toString() {
        return array.toString();
    }
}

Above code prints:

[1,2,3,4,5,6,7,8,9]
[3,4,5]
Sign up to request clarification or add additional context in comments.

Comments

2

with Gson

List<MyModel> myModelList = gson.fromJson(jsonArray.toString(), listType);

Updated

you can create a utility for the same if you only want to use javax.json package. Below is the complete sample code with only javax.json.* package.

import java.io.StringReader; import java.util.List;

import javax.json.Json; import javax.json.JsonArray; import javax.json.JsonObject; import javax.json.JsonReader; import javax.json.JsonValue;

public class Converter {
    public static void main(String[] args) {
        String str =     "{\r\n" + 
                         "\"name\":\"John\",\r\n" + 
                         "\"age\":30,\r\n" + 
                         "\"cars\":[ \"Ford\", \"BMW\", \"Fiat\" ]\r\n" + 
                         "}";

        JsonReader reader = Json.createReader(new StringReader(str));
        JsonObject object = reader.readObject();
        JsonArray array = object.getJsonArray("cars");
        System.out.println(getJsonArrayFromIndex(1, 2, array));
        System.out.println(getJsonArrayFromIndex(0, 2, array));
        System.out.println(getJsonArrayFromIndex(0, 1, array));
    }

    public static List<JsonValue> getJsonArrayFromIndex(int start, int end, JsonArray array) {
        return array.subList(start, end);       
    }
}

output

[BMW]
[Ford, BMW]
[Ford]

1 Comment

Please note that I can't use another package (such as google.gson). DId you read that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.