Rds-json generate json with index key on first level of array

Hello Nginx list

I’m using OpenResty with libdrizzle to provide a faster API to query
certain things from my db.

My current config is like this:

    location ~* ^/resty/usersTable/userId/([0-9\,]+)$  {
        set_unescape_uri $uid $1;
        drizzle_query 'select  id, nickname, age, age_p, city, plz,

wio_plz, gender from users where id in ($uid)’;
drizzle_pass projectdb;
rds_json on;
}

So this works fine and it gives me the expected output.

My problem is that if I query many user IDs i’m only getting a flat
array
of arrays without index key. But to improve the processing speed on the
client side, I would like to define that the ‘id’ field should be the
first
level index in the returned array. I am trying to show what I mean:

Current output:

[

{“id”:1971,“nickname”:“Robby1”,“age”:28,“age_p”:42,“city”:“Dresden”,“plz”:"",“wio_plz”:“2,4,5”,“gender”:“m”},

{“id”:1972,“nickname”:“Robby2”,“age”:29,“age_p”:43,“city”:“Dresden2”,“plz”:"",“wio_plz”:“4,5”,“gender”:“f”},

{“id”:1973,“nickname”:“Robby3”,“age”:30,“age_p”:44,“city”:“Dresden3”,“plz”:"",“wio_plz”:“5”,“gender”:“m”},
]

What I want:

[

1971:{“nickname”:“Robby1”,“age”:28,“age_p”:42,“city”:“Dresden”,“plz”:"",“wio_plz”:“2,4,5”,“gender”:“m”},

1972:{“nickname”:“Robby2”,“age”:29,“age_p”:43,“city”:“Dresden2”,“plz”:"",“wio_plz”:“4,5”,“gender”:“f”},

1973:{“nickname”:“Robby3”,“age”:30,“age_p”:44,“city”:“Dresden3”,“plz”:"",“wio_plz”:“5”,“gender”:“m”},
]

Is there some way how I can define a first level index key in rds-json?
I
already checked the rds_json_root parameter, but this doesn’t seem to
be
what I’m looking for.

Thanks,

Mauro

can use ngx_lua(openresty), filter/format the output.

smallfish http://chenxiaoyu.org

I just realized that I messed up the formatting. Actually what I’m
looking
for should be like this:

{
1971:{“nickname”:“Robby1”,“age”:28,“age_p”:42,“city”:“Dresden”,“plz”:“”,“wio_plz”:“2,4,5”,“gender”:“m”},
1972:{“nickname”:“Robby2”,“age”:29,“age_p”:43,“city”:“Dresden2”,“plz”:“”,“wio_plz”:“4,5”,“gender”:“f”},
1973:{“nickname”:“Robby3”,“age”:30,“age_p”:44,“city”:“Dresden3”,“plz”:“”,“wio_plz”:“5”,“gender”:“m”}
}

Posted at Nginx Forum:

Hello!

On Mon, Feb 25, 2013 at 12:56 AM, Mauro Stettler wrote:

Thanks, that would probably be possible.

It’s just that this URL is called very frequently, so I’m trying to do
everything as resource efficient as possible. I am worried that parsing the
json in lua in Nginx and reformatting it would be too heavy on the server
load. So I would have preferred if there is some way how I can tell the
rds-json module to format it the way I want.

No, ngx_rds_json does not support this very JSON structure. Because
there’s tons of different possible JSON structures, I don’t quite feel
like implementing complex configuration templates in ngx_rds_json in
pure C, which is a daunting task and adds complexity to this simple
module.

I believe using ngx_lua for complicated formatting requirements is the
right way to go.

Best regards,
-agentzh

Thanks, that would probably be possible.

It’s just that this URL is called very frequently, so I’m trying to do
everything as resource efficient as possible. I am worried that parsing
the
json in lua in Nginx and reformatting it would be too heavy on the
server
load. So I would have preferred if there is some way how I can tell the
rds-json module to format it the way I want.

Seems there is no way to configure it in the module, so I will look for
another solution.

Thanks,

Mauro

Thanks for the reply agentzh,

Ok, I’ll probably implement this either on lua in the Nginx or on the
client side inside the javascript then. Will have to do some tests of
both
solutions to decide.

Regards,

Mauro