0

I'm rendering some json that outputs this

 render json: service.get_object_item(param)
[
    [0] "{"object_item":[{"count":1,"dueDate":"2021-02-23","dayCreated":23}]}"
]

I want to change the object_item array key to camel case so it will match the inner array casing.

[
    [0] "{"objectItem":[{"count":1,"dueDate":"2021-02-23","dayCreated":23}]}"
]

However it seems like it gets set when render gets called so I can't change it. I've only been able to omit it by doing render json: service.get_object_item(param).to_json.

2
  • That looks sort of but not quite like JSON. What's that output format? Commented Feb 24, 2021 at 3:23
  • It's an array and the json is inisde the array at key object_item Commented Feb 24, 2021 at 17:12

2 Answers 2

1

I was able to solve it with this

render json: { objectItem: service.get_object_item(param) }

Which returns

[
    [0] "{"objectItem":[{"count":1,"dueDate":"2021-02-23","dayCreated":23}]}"
]
Sign up to request clarification or add additional context in comments.

Comments

0

You can either use active model serializers and define the keys there, or you can use rails built in transform_keys function

1 Comment

I tried the transform_keys method but that didn't work. I think because the thing i want to transform is the key of an array, not a hash. json[0].transform_keys{ |key| key.camelize} ` and json.transform_keys{ |key| key.camelize} both had errors

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.