Question

I have the following code which is taking results from an api call and providing it to my front-end as json:

notes = { :notes => [] }

json['Response']['OperationResponse']['Notes']['Note'].each do |note|
    notes[:notes] << [:user => note['User'], :time_stamp => note['TimeStamp'], :text => note['Text']]
end

render :json => notes.to_json

but I'm getting this format:

{
    "notes": [
        [
            {
                "time_stamp": "test",
                "user": "test",
                "text": "test"
            }
        ],
        [
            {
                "time_stamp": "test",
                "user": "test",
                "text": "test"
            }
        ],
        ....

Instead of this desired format:

{
    "notes": [
        [
            {
                "time_stamp": "test",
                "user": "test",
                "text": "test"
            },

            {
                "time_stamp": "test",
                "user": "test",
                "text": "test"
            },
            ....
Was it helpful?

Solution

Try with

json['Response']['OperationResponse']['Notes']['Note'].each do |note|
    notes[:notes] << {:user => note['User'], :time_stamp => note['TimeStamp'], :text => note['Text']}
end

You are pushing the element as an array [], use {} instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top