API Response JSON - How to determine suggested facet order

We are consuming the Search and Merch API in the backend, serialising the response into a concrete Java class when we receive it, and using it to generate our listing page facets. The problem with this approach is that, in doing so, we lose the facet field order–the order in which the AI suggests you should display them to the user.

Has anyone encountered a similar problem in the past, and if so how did you solve it?

2 Likes

I haven’ faced that problem, but, I guess storing a integer with insertion value would solve the problem, no?

@machak The problem is that the JSON which the Search & Merch API returns to us (which we have no control over) is stored as an object of which the properties are the facets. We can’t retain the order of the facets after serialisation.

Hi, if this is possible, could you post your POJO here? I guess it should contain a facet list, right?

Which Java JSON library and version are you using for deserialization? For example, by default, the Jackson library uses a LinkedHashMap when deserializing a hash such as “facet_fields” to retain order when iterated over. How is facetFields defined in your POJO? Thanks.

@matt.sidesinger I’m using Jackson 2.9.8 and the facet_fields object looks like so:

image

I should note that I am serialising JSON into a Java Object. AFAIK I won’t have access to any LinkedHashMap which retains the order.

Thanks

It looks like you defined a FacetFields class in your implementation. If you can, you could post both the setFacetFields method in your POJO and FaceFields constructors. Probably you are using a data type not preserving the insertion order…

@AproposArmadillo

If you define the facetFields instance variable and the associated getter/setter as a HashMap (the interface, not an explicit implementation), by default, Jackson should instantiate a LinkedHasMap, which preserves order when iterated over.

Thanks for all the input.

I ended up solving it by using the Gson library which allows you to convert JSON into a JSONObject which exactly mirrors the structure of the JSON, thus preserving the order.

I then just iterate over the “facet_fields” member.