tags:

views:

101

answers:

2

I have a document based on a xml structure that I have stored in a CouchDB database.

Some of the keys contains namespaces and are on the form "namespace:key":

{"mykey": {"nested:key": "nested value"}}

In the map function, I want to emit the nested value as a key, but the colon inside the name makes it hard...

emit(doc.mykey.nested:key, doc)   <-- will not work. 

Does anyone know how this can be solved?

A: 

That's because Couch is a JSON based document DB, and doc.mykey.nested:key is not a valid JSON identifier. JSON identifiers must match JavaScripts identifiers, and : is not a valid identifier character.

So, the simple answer is: "No, this won't and can't work". You need to change your identifiers.

Actually, I should qualify that.

Couch can use pretty much ANYTHING for it's views et al, and, in theory, works with any payload. But out of the box, it's just JavaScript and JSON.

Will Hartung
When reading on www.json.org, I can't find anything about valid identifiers. All I find, is that in a name:value-pair, the name should be a string, and that a string can contain any unicode character (but some, not including colon, must be escaped). Do you have any references that approve your statement?
Vegar
+4  A: 

A hint that its all just JSON and JavaScript got me some new ideas for searching.

It may be that colon in json keys ain't valid, but I found a way. By looking at the doc object as an hash, I can access my value in the following manner:

Doc.mykey['nested:key']

It works - for now...

Vegar
There is no appreciable difference between using the dot format vs. the subscript format. So that is the right way to handle it. `emit(doc.mykey["nested:key"], doc)` is just fine!
jhs