-1

I have a for loop in which I add some attributes to the object and at the end I need to have a string (to pass it as a query parameter)

I need

{"items":[{"itemsCode":"item1","isAvailable":false}],"isActive":false},{"items":[{"entryCode":"item2","isSelected":true}],"isActive":false}

When I try

List<List> itemsList= new ArrayList<>();
itemsList.add(new Items(items, isBoolean));
ObjectMapper mapper = new ObjectMapper();
String itemsJson = mapper.writeValueAsString(itemsList);

POJO

public class Items {

    private List<Item> items;
    private Boolean isBoolean;

I get this:

[{"items":[{"itemsCode":"item1","isAvailable":false}],"isActive":false},{"items":[{"entryCode":"item2","isSelected":true}],"isActive":false}]

please help how to get rid of an array brackets, but keeping the convention of adding those object to a list in a for loop

5
  • Why do you have a list (List<List> itemsList= new ArrayList<>();) if you don't want a list? Or vice versa, why don't you want a list, if you need a list?
    – tevemadar
    Commented Aug 22, 2023 at 10:25
  • I need two objects created with different values (in a loop) and them make it as a string
    – jacob1989
    Commented Aug 22, 2023 at 10:27
  • 1
    It's a string, if there's no better idea, you can always have a itemsJson = itemsJson.substring(1, itemsJson.length() - 1);.
    – tevemadar
    Commented Aug 22, 2023 at 10:33
  • What you are trying to generate isn't valid JSON, so a JSON library won't be able to generate it. If this really is what you want, consider serializing each Item to String separately, then joining those with commas. And then figure out how you're ever going to parse that string back into objects.
    – user8681
    Commented Aug 22, 2023 at 11:27
  • Your String form is JSON and your data model includes a List, so the square braces are correct. See json.org/json-en.html. If your intent is to output some non-JSON form then ObjectMapper (I assume this is the Jackson class) may not be a good solution.
    – vsfDawg
    Commented Aug 22, 2023 at 13:20

1 Answer 1

0

[] denote an array, so if your top-level object for serialization is List you cannot avoid having them. If you want to have top-level item as plain objects you need to rewrite the code adding one more POJO:

public class ItemsContainer {
    private List<Items> itemsList;
}

public class Items {
    private List<Item> items;
    private Boolean isBoolean;
}

List<Items> itemsList= new ArrayList<>();
itemsList.add(new Items(items, isBoolean));

ItemContainer container = new ItemContainer();
container.setItemsList(itemsList);

ObjectMapper mapper = new ObjectMapper();
String itemsJson = mapper.writeValueAsString(container);
2
  • it would be good idea but it adds ItemContainer to the front--> {"container":[{"items":[{"itemsCode":"item1","isAvailable":false}],"isActive":false},{"items":[{"entryCode":"item2","isSelected":true}],"isActive":false}]}
    – jacob1989
    Commented Aug 22, 2023 at 10:39
  • Otherwise you cannot avoid [] as you are serializing a Collection Commented Aug 22, 2023 at 11:05

Not the answer you're looking for? Browse other questions tagged or ask your own question.