how to return selected data on json api
i have created API i have two tables users and addresses i want only return state and city in seperate array how is it ?
my controller
def show
@address = Address.where(user_id:params[:id])
render json: @address
end
My postman output
https://imgur.com/a/t5xHYBI
#my expected output
[
{
"city": "palani",
"city": "coimbatore",
},
{
"state": "tamilnadu",
"state": "tamilnadu",
}
]
ruby-on-rails json ruby api ruby-on-rails-5
add a comment |
i have created API i have two tables users and addresses i want only return state and city in seperate array how is it ?
my controller
def show
@address = Address.where(user_id:params[:id])
render json: @address
end
My postman output
https://imgur.com/a/t5xHYBI
#my expected output
[
{
"city": "palani",
"city": "coimbatore",
},
{
"state": "tamilnadu",
"state": "tamilnadu",
}
]
ruby-on-rails json ruby api ruby-on-rails-5
use select option
– Nilay Singh
Nov 13 '18 at 7:47
Go through gem act_as_api. It is really very good gem to create json response for API.
– ray
Nov 13 '18 at 8:25
add a comment |
i have created API i have two tables users and addresses i want only return state and city in seperate array how is it ?
my controller
def show
@address = Address.where(user_id:params[:id])
render json: @address
end
My postman output
https://imgur.com/a/t5xHYBI
#my expected output
[
{
"city": "palani",
"city": "coimbatore",
},
{
"state": "tamilnadu",
"state": "tamilnadu",
}
]
ruby-on-rails json ruby api ruby-on-rails-5
i have created API i have two tables users and addresses i want only return state and city in seperate array how is it ?
my controller
def show
@address = Address.where(user_id:params[:id])
render json: @address
end
My postman output
https://imgur.com/a/t5xHYBI
#my expected output
[
{
"city": "palani",
"city": "coimbatore",
},
{
"state": "tamilnadu",
"state": "tamilnadu",
}
]
ruby-on-rails json ruby api ruby-on-rails-5
ruby-on-rails json ruby api ruby-on-rails-5
edited Nov 13 '18 at 9:00
giri dharan
asked Nov 13 '18 at 7:45
giri dharangiri dharan
2939
2939
use select option
– Nilay Singh
Nov 13 '18 at 7:47
Go through gem act_as_api. It is really very good gem to create json response for API.
– ray
Nov 13 '18 at 8:25
add a comment |
use select option
– Nilay Singh
Nov 13 '18 at 7:47
Go through gem act_as_api. It is really very good gem to create json response for API.
– ray
Nov 13 '18 at 8:25
use select option
– Nilay Singh
Nov 13 '18 at 7:47
use select option
– Nilay Singh
Nov 13 '18 at 7:47
Go through gem act_as_api. It is really very good gem to create json response for API.
– ray
Nov 13 '18 at 8:25
Go through gem act_as_api. It is really very good gem to create json response for API.
– ray
Nov 13 '18 at 8:25
add a comment |
2 Answers
2
active
oldest
votes
You will have to understand about JSON builders,
start here https://www.ruby-toolbox.com/categories/API_Builders
Choose one and get along .
I have used Jbuilder quite a few times
add a comment |
create json-jbuilder view
app/[controller]/show.json.jbuilder
json.id address.id
json.user_id address.user_id
json.city address.city
...
your controller action
def show
@address = Address.where(user_id:params[:id])
end
for more details you may read jBuilder documentation https://github.com/rails/jbuilder
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276132%2fhow-to-return-selected-data-on-json-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You will have to understand about JSON builders,
start here https://www.ruby-toolbox.com/categories/API_Builders
Choose one and get along .
I have used Jbuilder quite a few times
add a comment |
You will have to understand about JSON builders,
start here https://www.ruby-toolbox.com/categories/API_Builders
Choose one and get along .
I have used Jbuilder quite a few times
add a comment |
You will have to understand about JSON builders,
start here https://www.ruby-toolbox.com/categories/API_Builders
Choose one and get along .
I have used Jbuilder quite a few times
You will have to understand about JSON builders,
start here https://www.ruby-toolbox.com/categories/API_Builders
Choose one and get along .
I have used Jbuilder quite a few times
answered Nov 13 '18 at 7:54
NithinNithin
2,96231748
2,96231748
add a comment |
add a comment |
create json-jbuilder view
app/[controller]/show.json.jbuilder
json.id address.id
json.user_id address.user_id
json.city address.city
...
your controller action
def show
@address = Address.where(user_id:params[:id])
end
for more details you may read jBuilder documentation https://github.com/rails/jbuilder
add a comment |
create json-jbuilder view
app/[controller]/show.json.jbuilder
json.id address.id
json.user_id address.user_id
json.city address.city
...
your controller action
def show
@address = Address.where(user_id:params[:id])
end
for more details you may read jBuilder documentation https://github.com/rails/jbuilder
add a comment |
create json-jbuilder view
app/[controller]/show.json.jbuilder
json.id address.id
json.user_id address.user_id
json.city address.city
...
your controller action
def show
@address = Address.where(user_id:params[:id])
end
for more details you may read jBuilder documentation https://github.com/rails/jbuilder
create json-jbuilder view
app/[controller]/show.json.jbuilder
json.id address.id
json.user_id address.user_id
json.city address.city
...
your controller action
def show
@address = Address.where(user_id:params[:id])
end
for more details you may read jBuilder documentation https://github.com/rails/jbuilder
answered Nov 13 '18 at 7:54
Sikandar TariqSikandar Tariq
415414
415414
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53276132%2fhow-to-return-selected-data-on-json-api%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
use select option
– Nilay Singh
Nov 13 '18 at 7:47
Go through gem act_as_api. It is really very good gem to create json response for API.
– ray
Nov 13 '18 at 8:25