3

I have JSON data as follows-

var raw = {"raw":[{"W4Jlp9HKx_MVImNZuJwPqA!!:dgIN_ZskRAf5OtpgZ7tYLQ!!":{"name":"Name1","uniqID":"W4Jlp9HKx_MVImNZuJwPqA!!:dgIN_ZskRAf5OtpgZ7tYLQ!!"},"uFzQkOcsEwmRDGlob11Qiw!!:ArYiisKagqjnqMGfbKP7Yw!!":{"name":"Name2","uniqID":"uFzQkOcsEwmRDGlob11Qiw!!:ArYiisKagqjnqMGfbKP7Yw!!"},"fWcL6bvhCvp1wO95-7K3LA!!:XNYxDArq9E_6u2yaDbST0A!!":{"name":"Name3","uniqID":"fWcL6bvhCvp1wO95-7K3LA!!:XNYxDArq9E_6u2yaDbST0A!!"}}]}

The keys are dynamic, i.e., the first key "W4Jlp9HKx_MVImNZuJwPqA!!:dgIN_ZskRAf5OtpgZ7tYLQ!!" changes with every call we make to the JSON URL.

I want to parse the JSON data and store the data in Javascript variables.

I have tried to parse this with-

var arra = JSON.parse(raw);

And tried to get the value using

arra.raw[0].W4Jlp9HKx_MVImNZuJwPqA!!:dgIN_ZskRAf5OtpgZ7tYLQ!!.name

But this isn't helping, I do not get the values in this. I am not sure why this is happening as well. Is it because keys contain special characters?

It would be great if there is a way to list down the keys as well and fetch values with key index or something like that, i.e., something like

arra.raw[0].key(0).name

Wherein I don't have to give the key, as it changes with every call. Even if not a working solution using key name would suffice for now.

I'd prefer to achieve this in Javascript, if it isn't possible, please feel free to let me know in what language it can be achieved.

Thanks a bunch!

--- Edit ---

The answer to getting the value using key with special characters has been given in the comments, I had to use [] notation to fetch the result. Thanks a lot guys!! But the second question about fetching results using key position, than the key name remains open.


iMan
  • 456
  • 1
  • 7
  • 18
  • From https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json, you can use bracket notation: `arra.raw[0]["W4Jlp9HKx_MVImNZuJwPqA!!:dgIN_ZskRAf5OtpgZ7tYLQ!!"].name` – apsillers Jul 14 '17 at 17:07
  • 4
    Is it possible to change the JSON data source, because an object with random or unknowable property names is not that useful, an array would be much better. – James Jul 14 '17 at 17:08
  • !! Is a double not. Use bracket notation! – Jonas Wilms Jul 14 '17 at 17:08
  • [MDN: Working with objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects) – Andreas Jul 14 '17 at 17:08
  • @James No, unfortunately, we have no control over the source. :/ – iMan Jul 14 '17 at 17:12
  • @apsillers thanks a lot! – iMan Jul 14 '17 at 17:12

2 Answers2

2

Here you go with one more solution https://jsfiddle.net/bm1n59hf/1/

var data = {"raw":[{"W4Jlp9HKx_MVImNZuJwPqA!!:dgIN_ZskRAf5OtpgZ7tYLQ!!":{"name":"Name1","uniqID":"W4Jlp9HKx_MVImNZuJwPqA!!:dgIN_ZskRAf5OtpgZ7tYLQ!!"},"uFzQkOcsEwmRDGlob11Qiw!!:ArYiisKagqjnqMGfbKP7Yw!!":{"name":"Name2","uniqID":"uFzQkOcsEwmRDGlob11Qiw!!:ArYiisKagqjnqMGfbKP7Yw!!"},"fWcL6bvhCvp1wO95-7K3LA!!:XNYxDArq9E_6u2yaDbST0A!!":{"name":"Name3","uniqID":"fWcL6bvhCvp1wO95-7K3LA!!:XNYxDArq9E_6u2yaDbST0A!!"}}]};

for(var key in data.raw[0]){
 console.log("KEY: " + key + "    Name: " + data.raw[0][key].name + "   ");
}
Shiladitya
  • 12,003
  • 15
  • 25
  • 38
1
arra.raw[0].key(0).name

May work if you implement key before:

Object.defineProperty(Object.prototype,"key",{
 enumerable:false,
 value: function(key){
    return this[Object.keys(this)[key]||key];
  }
});

Or if you dont mind using the native way:

Object.values(arra.raw[0])[0].name
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
  • Thank you, I will try that. Though I don't exactly understand what is happen in that function. – iMan Jul 14 '17 at 17:11
  • Cool, the only confusing part is that now arrays, objects, strings, functions ... will have `keys` method. – dfsq Jul 14 '17 at 17:16
  • 1
    @dfsq *key* , *keys* would probably collide with Maps ... And it may be useful if its used multiple times – Jonas Wilms Jul 14 '17 at 17:19