3

My resulting json looks like this:

{"popularPurchases":[
     {"product":{"id":"123","face":"face1","size":"1","price":"500"},
       "recent":["smith","hambone","someone"]},
     {"product":{"id":"222","face":"face2","size":"2","price":"600"},
       "recent":["john","mary"]}
]}

My question is how can I get rid of the labels "popularPurchases" (including the ':') and "product (including the ':')?

My POJOs all look the same. Here is one of them:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class PopularPurchase {

    @XmlElement
    private Product product;

    @XmlElement(name="recent")
    private List<String> users;

    public Product getProduct() {
        return product;
    }
    public void setProduct(Product product) {
        this.product = product;
    }
    public List<String> getUsers() {
        return users;
    }
    public void setUsers(List<String> users) {
        this.users = users;
    }   
}

I need it to look like this:

[
  {"id":"123","face":"face1","size":"1","price":"500"},
    "recent":["smith","hambone","someone"]},
  {"id":"222","face":"face2","size":"2","price":"600"},
       "recent":["john","mary"]}
]

I am producing the JSON with:

PopularPurchaseResponse ppResponse = new PopularPurchaseResponse();
ppResponse.setPopularPurchases(popularPurchases);       

builder = Response.status(200).entity(ppResponse);

return builder.build();
Mike A
  • 71
  • 7
  • How are you producing the JSON? – Adam Jan 31 '17 at 21:42
  • PopularPurchaseResponse ppResponse = new PopularPurchaseResponse(); ppResponse.setPopularPurchases(popularPurchases); builder = Response.status(200).entity(ppResponse); return builder.build(); Sorry about the formatting. See above – Mike A Jan 31 '17 at 22:14
  • I am assuming that is what you are returning from an endpoint? Is Jackson handling the conversion to JSON? – Adam Jan 31 '17 at 22:40
  • Yes. That code is at the end. Yes. I am using Jackson. – Mike A Jan 31 '17 at 22:41
  • Have you tried configuring your mapper? `mapper.configure(SerializationFeature.WRAP_ROOT_VALUE, false);` See here: http://stackoverflow.com/questions/8837018/jackson-json-deserialization-ignore-root-element-from-json – Adam Jan 31 '17 at 22:45
  • I have not. Does this require a different way to return the result other than the above? This seems like it would affect all of the root value wrapping. I still need the 'recent' label. – Mike A Jan 31 '17 at 22:54
  • You could try to add @XMLTransient annotation on those fields/get methods that you don't want to be serialized to JSON. – FrAn Jan 31 '17 at 22:58
  • http://stackoverflow.com/q/32097870/2587435 – Paul Samsotha Feb 01 '17 at 01:12
  • I want all of the fields I don't want the [label] : in front of some of it – Mike A Feb 01 '17 at 01:18

0 Answers0