ejang
ejang

Reputation: 4062

javascript retrieve value from JSON object by matching key using Regex

I have the following javascript object literal (excerpt)

var foo = {"hello[35]":100,"goodbye[45]":42};

I have the following query:

var query = "hello"

I would like to call foo[query] to obtain the value 100, but there is a [35] for which I don't necessarily know the value of. I know for sure that I will get a unique match. Is there any way to input query is some kind of javascript regular expression? i.e.

Regex = /hello/
foo[Regex]
100

pardon the noob question...

Upvotes: 3

Views: 15526

Answers (6)

Diode
Diode

Reputation: 25135

function getVal(s, q){
   var r = s.match(new RegExp(q + "\\[\\d*\\]\":(\\d*)[\\,\\}]"));
   return r?r.pop():false;     
}
getVal(foo, "hello")

Upvotes: 0

Tim Vermaelen
Tim Vermaelen

Reputation: 7059

Not sure if you can use regex without any plugins or so ... This might help already ...

var foo = {"hello[35]":100,"goodbye[45]":42};
var query = "hello";
for(var key in foo){
    if (key.indexOf(query) > -1)
        document.write(foo[key]);
}

http://jsfiddle.net/3qqSr

Upvotes: 2

nnnnnn
nnnnnn

Reputation: 150030

What you have here:

var foo = {"hello[35]":100,"goodbye[45]":42};

is not JSON, which is a string representation of an object; what you have is an object literal, which creates an actual JavaScript object. As far as I know the only way to retrieve a value from an object by matching a property name with a regex is to enumerate the property names and test each one. The regex you'll need is something like:

/^hello(\[\d*\])?$/

...which will match against "hello" optionally followed by zero or more digits in square brackets. But you don't want to hard code "hello" given that you also (presumably) need the "goodbye" value, so use a function:

function getPropertyByRegex(obj,propName) {
   var re = new RegExp("^" + propName + "(\\[\\d*\\])?$"),
       key;
   for (key in obj)
      if (re.test(key))
         return obj[key];
   return null; // put your default "not found" return value here
}

var foo = {"hello[35]":100,"goodbye[45]":42};

alert(getPropertyByRegex(foo, "hello"));    // 100
alert(getPropertyByRegex(foo, "goodbye"));  // 42
alert(getPropertyByRegex(foo, "whatever")); // null (not found)

Demo: http://jsfiddle.net/asDQm/

Upvotes: 6

InuiSadaharu
InuiSadaharu

Reputation: 11

var foo = {"hello[35]":100,"goodbye[45]":42};

foo = foo.replace(/\[\d+\]/g,'');

var obj = (new Function("return "+foo))();

obj.hello -> 100

obj.goodbye -> 42

var query = 'hello';

obj[query] -> 100

Upvotes: 0

JMax
JMax

Reputation: 26591

As your JSON is a string, you can use a regexp with this kind of statement:

var foo = '{"hello[35]":100,"goodbye[45]":42}';
var result = foo.match(/"hello\[\d*\]":\d*/g);
result = result[0].split(":")[1];
alert(result);

See it live on jsfiddle

Note that you could use a var instead of "hello" in your regexp.

Upvotes: 0

Shadow
Shadow

Reputation: 6277

I am noob here too but I have seen this page previously see it helps you with your question. It basically explains JSon path. Also see this question.

Upvotes: 0

Related Questions