How do I parse a JSON API style query in JavaScript? An example query would be:
/search?filter[industries.id]=1&filter[locations.id]=3&filter[item_type][0]=temporary
I want to extract the filter fields and value (and ideally other JSON API parameters). I don’t mind too much what format the result is in, whether it’s an oject or array etc. I need to be able to check if a filter exists and get it’s value.
This is client side, not on the server.
Happy to use a library is there is one, but can’t find one.
2
Answers
The easiest and obvious way to handle such requests would be to convert it into JSON like this:
Here is an example of code to do it
You can use the browser’s URLSearchParams interface to do most of the work. Here’s one way that includes splitting the key (like, "filter[industries.id]") into two separate properties.