API Response JSON - How to determine suggested facet order

#1

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
#2

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

#3

@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.

#4

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

#5

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.

#6

@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

#7

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…

#8

@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.

#9

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.