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

**URL:** <https://www.ruby-forum.com/t/rds-json-generate-json-with-index-key-on-first-level-of-array/226901>\
**Category:** NGINX\
**Created:** [February 25, 2013, 8:53am UTC](https://www.ruby-forum.com/t/rds-json-generate-json-with-index-key-on-first-level-of-array/226901 "2013-02-25T08:53:54Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [February 25, 2013, 8:53am UTC](https://www.ruby-forum.com/t/rds-json-generate-json-with-index-key-on-first-level-of-array/226901/1 "2013-02-25T08:53:54Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [February 25, 2013, 9:12am UTC](https://www.ruby-forum.com/t/rds-json-generate-json-with-index-key-on-first-level-of-array/226901/2 "2013-02-25T09:12:35Z")

</div>

## can use ngx\_lua(openresty), filter/format the output.

smallfish [http://chenxiaoyu.org](http://chenxiaoyu.org)

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [February 25, 2013, 9:05am UTC](https://www.ruby-forum.com/t/rds-json-generate-json-with-index-key-on-first-level-of-array/226901/3 "2013-02-25T09:05:00Z")

</div>

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:

> **[Re: rds-json generate json with index key on first level of array](https://forum.nginx.org/read.php?2,236563,236564#msg-236564)**
>
> 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;

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [February 25, 2013, 9:04pm UTC](https://www.ruby-forum.com/t/rds-json-generate-json-with-index-key-on-first-level-of-array/226901/4 "2013-02-25T21:04:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [February 25, 2013, 9:57am UTC](https://www.ruby-forum.com/t/rds-json-generate-json-with-index-key-on-first-level-of-array/226901/5 "2013-02-25T09:57:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Mauro\_Stettler](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/898d66/32.png) [@Mauro\_Stettler](https://www.ruby-forum.com/u/Mauro_Stettler)\
**Post date:** [February 27, 2013, 5:57am UTC](https://www.ruby-forum.com/t/rds-json-generate-json-with-index-key-on-first-level-of-array/226901/6 "2013-02-27T05:57:03Z")

</div>

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
