You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by bram <bn...@gmail.com> on 2010/01/20 13:57:10 UTC

JSON serialization in views

Hi all,

I'm new to CouchDB and I ran into a small hick-up. I would like to hear
any opinions about my approach and any possible solutions I do not
mention myself ;)

Introduction: I decided to write a script to push /proc information into
a CouchDB instance and then use the map reduce to create graph data,
mean loads, etc. (Code is GPLv3, available at [1])

Approach: I have a json object within my entries to document where the
information came from (called acquire) and I would like to use a
concatenation of that information as a key in the emit() call. I decided
to try out: emit(doc.acquire, doc)

I like using doc.acquire directly because I don't have to concatenate
all the different fields by hand and it keeps it all readable and very
short.

Problem: using a json object as a key value in emit will serialize it,
but the serialization does not seem to be stable, so serializing the
object {a:2,b:3} and {b:3, a:2} will not result in the same string.

Question: Is there a json serialization function/call that I can use to
sort on object attribute names, to make sure that {a:2,b:3} and {b:3,
a:2} will serialize to the same string?


Greets,
  Bram Neijt


[1] http://git.logfish.net/?p=procplumbing.git;a=summary


Re: JSON serialization in views

Posted by Zachary Zolton <za...@gmail.com>.
Bram,

Serialized arrays do retain their order, and work for view keys. So,
let's say your object looks like this:
{a:2,b:3}

You could use a function to turn each attribute into its own key-value
object, in an array:
[{a:2},{b:3}]

The trick to creating these keys is to use an arbitrary ordering. So,
I'd suggest you just sort the array via each object's sole attribute
name.


Cheers,

Zach


On Wed, Jan 20, 2010 at 6:57 AM, bram <bn...@gmail.com> wrote:
> Hi all,
>
> I'm new to CouchDB and I ran into a small hick-up. I would like to hear
> any opinions about my approach and any possible solutions I do not
> mention myself ;)
>
> Introduction: I decided to write a script to push /proc information into
> a CouchDB instance and then use the map reduce to create graph data,
> mean loads, etc. (Code is GPLv3, available at [1])
>
> Approach: I have a json object within my entries to document where the
> information came from (called acquire) and I would like to use a
> concatenation of that information as a key in the emit() call. I decided
> to try out: emit(doc.acquire, doc)
>
> I like using doc.acquire directly because I don't have to concatenate
> all the different fields by hand and it keeps it all readable and very
> short.
>
> Problem: using a json object as a key value in emit will serialize it,
> but the serialization does not seem to be stable, so serializing the
> object {a:2,b:3} and {b:3, a:2} will not result in the same string.
>
> Question: Is there a json serialization function/call that I can use to
> sort on object attribute names, to make sure that {a:2,b:3} and {b:3,
> a:2} will serialize to the same string?
>
>
> Greets,
>  Bram Neijt
>
>
> [1] http://git.logfish.net/?p=procplumbing.git;a=summary
>
>