Andrés Méndez
Posts: 0
Joined: Thu Apr 18, 2013 6:13 pm

Filter JSON response

Hello.

I have a JSON response with a list of items (and data of each item). For example:
code[
{
"titulo": "Cazadora",
"precio": 10,
"slug": "SRbEZFaZ3M5pc",
"descripcion": "Cazadora finita de entretiempo"
},
{
"titulo": "Coche",
"precio": 5,
"slug": "FRbEZFaZ3M5p3",
"descripcion": "Coche RC a pilas"
}
]/code

Is it possible to filter the JSON response to use only the data of the item that I need (selecting an unique identifier as "slug")?

Thanks.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Filter JSON response

Sure, you can map just the 'slug' value or modify the service response to return just the 'slug'.

Andrés Méndez
Posts: 0
Joined: Thu Apr 18, 2013 6:13 pm

Filter JSON response

Thanks for the fast answer Max!

I'm a newbie, how is it done? Now, I assign the $ and the elements of the JSON response and I create several items, each one for each item in the JSON response.

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Filter JSON response

Create response automatically (see docs: http://docs.appery.io), then delete the items you don't need (or just don't map them). You still get the full response from the service, you are just not displaying all in a page.

Andrés Méndez
Posts: 0
Joined: Thu Apr 18, 2013 6:13 pm

Filter JSON response

Sorry Max, but I already read the documentation and couldn't find an example of doing this (maybe something near at http://docs.appery.io/tutorials/build...).

Can you be more specific or give an example please?

Thanks.

Sergey Kozyr
Posts: 0
Joined: Tue Apr 30, 2013 2:55 pm

Filter JSON response

You are able to remove unused response parameters at Rest Service "Response" settings tab. For example here is Twitter response structure where only few parameters are used:
Image

Andrés Méndez
Posts: 0
Joined: Thu Apr 18, 2013 6:13 pm

Filter JSON response

I think that I haven't explained correctly what I want.

I don't want to use only a few name/value from the JSON response, I want to use just one object.

I'll try to explain with an example.

I receive this:
code[
{
"titulo": "Cazadora",
"precio": 10,
"slug": "SRbEZFaZ3M5pc",
"descripcion": "Cazadora finita de entretiempo"
},
{
"titulo": "Coche",
"precio": 5,
"slug": "FRbEZFaZ3M5p3",
"descripcion": "Coche RC a pilas"
}
]/code

But I want only the object with slug "SRbEZFaZ3M5pc", so I would like to get this:
code[
{
"titulo": "Cazadora",
"precio": 10,
"slug": "SRbEZFaZ3M5pc",
"descripcion": "Cazadora finita de entretiempo"
}
]/code

maxkatz
Posts: 0
Joined: Fri Aug 13, 2010 3:24 pm

Filter JSON response

If you know the index, you can try this:

result

A better approach would be to return just the object you need from the service.

Andrés Méndez
Posts: 0
Joined: Thu Apr 18, 2013 6:13 pm

Filter JSON response

Hello.

In the association of JSON elements and page elements, I have added the following javascript to the $ response:
codelistas_filtradas = jQuery.grep(value, function(elemento) {
return ( elemento.slug == "SRbEZFaZ3M5pc" );
});
return listas_filtradas;/code

But it doesn't work, I see all the items.

Kateryna Grynko
Posts: 0
Joined: Thu Nov 15, 2012 9:13 am

Filter JSON response

Hi Andrés,

You cannot realize it this way. You can use Generic Rest Service to filter data got from Rest Service.

You can find an example here: https://getsatisfaction.com/apperyio/...

Return to “Issues”