[ckan-dev] Storing JSON in resource metadata

Hendrik Bunke bunke.hendrik at gmail.com
Tue Jun 16 12:34:35 UTC 2015


--On 2015-06-16 07:41, Ian Ward wrote:
> Would you share the code you're working on?
> 
> In my experience it's easier to store json in the resource extras because
> they are all stored internally as elements in a json object instead of rows
> in an extras table.

Ian, thanks for answering. Actually, I am storing field in
extras. Problem is that it works with datasets, but not with
resources.

The relevant code snippets are as follows:

plugin:
=======

schema['resources'].update({
      'authors': [tk.get_validator('ignore_missing'),
          tk.get_validator('authors'),
      ]
})

(it doesn't change anything using the 'convert_to_extras' converter here).


validator:
==========

def _grouper(seq, size):
    bargs = [iter(seq)] * size
    return izip_longest(*bargs)

def _dicter(seq, ids):
    return map(lambda t: dict(zip(ids, t)), seq)

def authors(value):
    """
    transform author fields to JSON string and store it
    """
    fields = author_fields() #coming from schema module
    dl = _dicter(_grouper(value[:], len(fields)), [i.id for i in fields])
    return json.dumps(dl)

This code, essentially, works for datasets but not for resources. That's why I
was assuming that storing JSON in resources extra fields is not supported yet.
Did you manage it?

thanks
hendrik

-- 
Dr. Hendrik Bunke
http://gplus.to/hbunke
http://twitter.com/hbunke
http://www.hbxt.org



More information about the ckan-dev mailing list