0

I try to make a JSON with a 3 propertys, "items", "status" and "id", but if i use @JsonValue, my JSON don't take "status" and "id"; How can i make this, it's possible?

I want My JSON this way without property name "items"

{
  [
    {
      "property1": "property",
      "property2": "property"
    }
  ],
  "Status": {
    "code": "code",
    "messages": [
      {
        "message1": "message",
        "message2": "message"
      }
    ]
  },
  "id": "123"
}

instead of this

{
  "items": [
    {
      "property1": "property",
      "property2": "property"
    }
  ],
  "Status": {
    "code": "code",
    "messages": [
      {
        "message1": "message",
        "message2": "message"
      }
    ]
  },
  "id": "123"
}

My classes

public class RetrieveResponse {
//@JsonUnwrapped even i try this.
@JsonValue
 private List<Response> items;
 private String Status status;
 private String id;

}
public class Status {
 private String code code;
 private List<Message> message;

}

And finally try with serializer but if i have more list the service its sending me a exception

public class ResponseSerializer extends JsonSerializer<List<Response>> {
    public static final TypeFactory TYPE_FACTORY = TypeFactory.defaultInstance();
    public static final JavaType LIST_TYPE = TYPE_FACTORY.constructCollectionType(List.class, Response.class);


    @Override
    public void serialize(List<Response> responses, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
        jsonGenerator.writeStartArray();
        for (Response item : responses) {
            responses.remove("priceConcept");
            jsonGenerator.writeObject(item);
        }
        jsonGenerator.writeEndArray();
    }

    @Override
    public Class<List<Response>> handledType() {
        return (Class<List<Response>>) LIST_TYPE.getRawClass();
    }
}

3
  • 1
    That is invalid JSON. Why do you want to do this? Commented Jul 3, 2023 at 1:08
  • Because at work they asked me for this and I didn't know if this is completely valid, but it's possible do that? Commented Jul 3, 2023 at 1:15
  • You need to remove the array and object brackets to make it a valid JSON, or you provide a valid key. The arrays are a bit of a non-sense at all, because the contain only one (object) element. Commented Jul 3, 2023 at 4:33

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.