How to deserialize a non standard JSON request in Rails? (non-JSONAPI)

49 views Asked by At

In a Rails app, I'm receiving a JSON request to track a Order model. I need to save all this to the DB. I'm not sure how to handle this input into the controller.

The JSON request body is in this format:

{
  "OrderID": "00023-00001",
  "OrderType": "Delivery",
  "DateTimes": {
    "DeliverDueDateTime": "2024/03/17 20:00:00"
  },
  "Area": {
    "Code": {
      "ZipCode": "1111",
      "AdditionalCodes": {
        "ZipCode": [
          "2111",
          "3111"
        ]
      }
    }
  }
}

This is a different format than what I'm used to (still new to this framework).

Most of the tutorials I found work with JSON keys in snake case, have either the order or data at the root, and don't contain nested data. Normally, I would do params.require(:order).permit(:request_number, ...). But here it's a bit different.

This JSON is different because:

  • it has nested keys
  • doesn't have anything at the root
  • has Pascal case keys

How should I handle this?

I checked the ActiveModel Serializer, but it doesn't seem to offer deserialization in this case. Unless I'm missing something.

https://github.com/rails-api/active_model_serializers/blob/v0.10.6/docs/general/deserialization.md

I tried things like:

params.permit(:OrderID, :OrderType, :Area)

params.permit(:OrderID, :OrderType).require(:Area).permit(:Code)

params.permit(:OrderID, :OrderType).require(:Area)

# ActionController::ParameterMissing: param is missing or the value is empty: Area
0

There are 0 answers