tags:

views:

70

answers:

3

I have an input like this (a JSON format)

{"location":[{"id":"1BCDEFGHIJKLM","somename":"abcd","fewname":"xyzland","sid":"","sname":"","regionname":"Zee-Whole","type":"some","siteCode":"","someCode":"ROTXY","fewCode":"NL","pCode":"ROTXY","someid":"1BCDEFGHIJKLM","fewid":"GIC8"},{"id":"7823XYZHMOPRE","somename":"abcd Junction","fewname":"United States","sid":"","sname":"","regionname":"New York","type":"some","siteCode":"","someCode":"USRTJ","fewCode":"US","pCode":"USNWK","someid":"7823XYZHMOPRE","fewid":"7823XYZLMOPRE"},{"id":"799XYZHMOPRE","somename":"abcd-Maasvlakte","fewname":"xyzland","sid":"","sname":"","regionname":"Zee-Whole","type":"some","siteCode":"","someCode":"XYROT","fewCode":"NL","pCode":"","someid":"799XYZHMOPRE","fewid":"OIUOWER348534"}]}

Now, I want to pick up the first "id" value which is 1BCDEFGHIJKLM using regular expressions. I have managed upto the point using

[^({"location":[?{"id":")].{0,12} but this is incomplete. Could some one help how do I ignore the rest of the line after the value 1BCDEFGHIJKLM

+6  A: 

Regex isn't the way to do this. Whatever platform you are using, it must have a JSON parser. That will be your best error-free solution.


Assuming you must use regex, you can grab all the id's using "id":"(.*?)", and take the first match.

I found the following article, which might help you.

Am
Absolutely. Unless his/her platform is a text editor...
Tim Pietzcker
Some text editors have built-in parsers, so i still think his using the wrong tool for the job.
Am
I am getting this input from JMeter as a response. And I need this specific value to go as an input to another http request.
Bala
via shell? or some script?
Am
This is basically the response visiting an url
Bala
I meant how are u parsing it? i never used JMeter, but i assume you get the result to some file and then parse it, or is it enclosed in some app
Am
There is a RegExp Tester built into JMeter that can be used to test regular expressions in response and/or request data
Bala
+1  A: 

While messy, how is your regex incomplete?

It could be shortened to ("id":"([^"]+)") which is more readable, and doesn't limit the ID to twelve characters. If that is beneficial.

If you problem is getting more than one result, most languages have a "g" global switch.

In javascript, the following would return "1BCDEFGHIJKLM":

var firstID = str.match(/"id":"([^"]+)"/)[1]

As match()returns an array, in which [0] is the entire returned string, and [1] the first parenthasis.

SamGoody
+1  A: 

Don't have to use regex. In your favourite language, split on commas. Then go through each item, check for "id" and split on colon (:). Get the last element. Eg Python

>>> s
'{"location":[{"id":"1BCDEFGHIJKLM","somename":"abcd","fewname":"xyzland","sid":"","sname":"","regionname":"Zee-Whole","type":"some","siteCode":"","someCode":"ROTXY","fewCode":"NL","pCode":"ROTXY","someid":"1BCDEFGHIJKLM","fewid":"GIC8"},{"id":"7823XYZHMOPRE","somename":"abcd Junction","fewname":"United States","sid":"","sname":"","regionname":"New York","type":"some","siteCode":"","someCode":"USRTJ","fewCode":"US","pCode":"USNWK","someid":"7823XYZHMOPRE","fewid":"7823XYZLMOPRE"},{"id":"799XYZHMOPRE","somename":"abcd-Maasvlakte","fewname":"xyzland","sid":"","sname":"","regionname":"Zee-Whole","type":"some","siteCode":"","someCode":"XYROT","fewCode":"NL","pCode":"","someid":"799XYZHMOPRE","fewid":"OIUOWER348534"}]}'

>>> for i in s.split(","):
...     if '"id"' in i:
...         print i.split(":")[-1]
...         break
...
"1BCDEFGHIJKLM"

Of course, ideally, you should use a dedicated JSON parser.

ghostdog74