3

I'm using JBuilder to build JSON response and now it look like:

 json.array!(@work_posts) do |work_post|
 json.extract! work_post, :post_title, :post_body, :salary, :urgently, :contact, :created_at, :updated_at
 json.contact do
   json.emails work_post.contact.emails
   json.phones work_post.contact.phones
   json.links work_post.contact.links
 end
end

And response look like:

[
  {
    "post_title": "Some work",
    "post_body": "work description",
    "salary": "5$/hour",
    "urgently": true,
    "contact": {
         "emails": "",
         "phones": "",
         "links": ""
              },
    "created_at": "2015-10-11T23:46:17.979+05:00",
    "updated_at": "2015-10-11T23:46:17.979+05:00"
    }
]

I want add cutom keys to make response look like:

"result" : "success",   
"data" : [
  {
    "post_title": "Some work",
    "post_body": "work description",
    "salary": "5$/hour",
    "urgently": true,
    "contact": {
         "emails": "",
         "phones": "",
         "links": ""
              },
    "created_at": "2015-10-11T23:46:17.979+05:00",
    "updated_at": "2015-10-11T23:46:17.979+05:00"
    }
]

What should I do to achieve this?

1 Answer 1

1

I haven't seen such question anywhere and I finally achieved what I wanted. Hope that it will be useful for someone. So the answer is very obvious: instead of using json.array! I should use what I exactly need => json.data and then I just need to put json.result "success" above. The result will be:

"result" : "success",   
"data" : [
 {
    "post_title": "Some work",
    "post_body": "work description",
    "salary": "5$/hour",
    "urgently": true,
    "contact": {
         "emails": "",
         "phones": "",
         "links": ""
          },
    "created_at": "2015-10-11T23:46:17.979+05:00",
    "updated_at": "2015-10-11T23:46:17.979+05:00"
  }
]
Sign up to request clarification or add additional context in comments.

Comments

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.