Thursday, January 31, 2013

Json object from an array of key value pairs

I mentioned this the other day, and I now have an extension with a simple function to turn an array of key value pairs into a json object, much as you can turn such a thing into an hstore.

The motivation for this came from a discussion on what to do with structured data returned by the Redis Foreign Data Wrapper. Originally we discussed making it json or an hstore, but on reflection it seemed best to me simply to return it as a standard SQL array and use the database utilities to transform it into whatever is needed. So this function works pretty much like the hstore function except that here it is the caller's responsibility to ensure that the keys are unique. When used with the Redis FDW that won't be a problem - they will always be unique.

3 comments:

  1. I'm trying to read the C code in your extension, and I feel like I just started watching some TV series in the very last season.

    For example, what does PG_MODULE_MAGIC; mean?

    ReplyDelete
  2. See http://www.postgresql.org/docs/current/static/xfunc-c.html Essentially it's a way of making sure that the module is compiled against the right version of Postgres.

    ReplyDelete