4
Convert JSON (key/value pairs) to two native arrays, one array of keys and another of values, in your language.
var X = '{"a":"a","b":"b","c":"c","d":"d","e":"e","f":"f9","g":"g2","h":"h1"}';
The value array could be an array of strings or integers.
So we need two functions keys & vals, returning native arrays on input of JSON string.
In above example the output would be:
keys : ["a", "b", "c", "d", "e", "f", "g", "h"]
vals : ["a", "b", "c", "d", "e", "f9", "g2", "h1"]
Here is my attempt at this using javascript:
keys : (53 Chars)
function keys(X){return X.match(/[a-z0-9]+(?=":)/gi)}
vals : (56 Chars)
function vals(X){return X.match(/[a-z0-9]+(?="[,}])/gi)}
Can other languages challenge this??
1What assumptions are you making about the input? You seem to be assuming that it's a single JSON object all of whose member values are strings, and none of whose names or values require escaping. Those assumptions seem to be rather unmotivated / arbitrary. – Peter Taylor – 2013-03-15T14:00:49.680
I know. But you could say it's a JSON with some constraints. It need not be arbitrary since we could find such cases a lot, in a practical data set. Besides, answers handling the general set would be more than welcome... (as in one of the answers) – loxxy – 2013-03-15T15:50:33.447
4
function keys(x){return Object.keys(JSON.parse(x))}
... – Shmiddty – 2013-03-15T21:55:12.933