Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get index of a key in json

I have a json like so:

json = { "key1" : "watevr1", "key2" : "watevr2", "key3" : "watevr3" } 

Now, I want to know the index of a key, say "key2" in json - which is 1. Is there a way?

like image 723
shreyj Avatar asked Mar 05 '13 07:03

shreyj


People also ask

How can I get the index from a JSON object?

To get the index from a JSON object with value with JavaScript, we can use the array findIndex method.

Does JSON have index?

You can index JSON data as you would any data of the type that you use to store it. In particular, you can use a B-tree index or a bitmap index for SQL/JSON function json_value , and you can use a bitmap index for SQL/JSON conditions is json , is not json , and json_exists .

Can a value be the key in JSON?

Keys must be strings, and values must be a valid JSON data type: string. number.


1 Answers

Its too late, but it may be simple and useful

var json = { "key1" : "watevr1", "key2" : "watevr2", "key3" : "watevr3" }; var keytoFind = "key2"; var index = Object.keys(json).indexOf(keytoFind); alert(index); 
like image 165
Gowsikan Avatar answered Oct 14 '22 02:10

Gowsikan