0

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"
            },
            ....

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

ah, I thought it would be something I was missing but didn't think it'd be as simple as that! Thanks.
Glad to Help. Please accept the answer after time out is over. :)

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.