Re: Extracting an expression/value from a string

1,091 views
Skip to first unread message

Bill Christensen

unread,
Dec 30, 2014, 6:11:32 PM12/30/14
to robotframework-users
I'm a bit rusty on my GREP skills, but in general I'd approach this by selecting all the characters up to the first comma, then remove the first 7 characters (  {"id":" ) and the last character (").  

This of course assumes the ID always comes first in your output string.  

If the order varies you'd have to first find the string that is between "id":" and the next instance of  ",   That should actually be more efficient, as you should be able to do that in one step.  

This also assumes that no other part of the returned string will ever contain the particular string "id":" and that your target string will never contain ",



On Tue, Dec 30, 2014 at 12:13 PM, komodospider <sbk...@gmail.com> wrote:
Hi!

I receive a return value from a RF library like this {"id":"7e48b23e-5c52-46a5-be6b-6cf904a2b3c3","name":"Some","created":"2014-12-30T18:00:32.349Z","updated":"2014-12-30T18:00:32.349Z","tags":[],"type":"All","parameters":{"Scope":"*","Target":"ALL"}}
That *should ideally* be a json formatted string, but as far as I can see it is just a string. And I'm trying to extract the value of the 'id'. Thus, 7e48b23e-5c52-46a5-be6b-6cf904a2b3c3
The string generated above is random and will be different each time, except the keys that will remain the same
I cannot figure this out. Anybody who can help?

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-u...@googlegroups.com.
To post to this group, send email to robotframe...@googlegroups.com.
Visit this group at http://groups.google.com/group/robotframework-users.
For more options, visit https://groups.google.com/d/optout.

Guy Kisel

unread,
Dec 30, 2014, 7:07:57 PM12/30/14
to robotframe...@googlegroups.com
If it's valid JSON, Python can convert it to a dictionary. https://docs.python.org/2/library/json.html#json.loads


On Tuesday, December 30, 2014 3:11:32 PM UTC-8, Bill Christensen wrote:
I'm a bit rusty on my GREP skills, but in general I'd approach this by selecting all the characters up to the first comma, then remove the first 7 characters (  {"id":" ) and the last character (").  

This of course assumes the ID always comes first in your output string.  

If the order varies you'd have to first find the string that is between "id":" and the next instance of  ",   That should actually be more efficient, as you should be able to do that in one step.  

This also assumes that no other part of the returned string will ever contain the particular string "id":" and that your target string will never contain ",


On Tue, Dec 30, 2014 at 12:13 PM, komodospider <sbk...@gmail.com> wrote:
Hi!

I receive a return value from a RF library like this {"id":"7e48b23e-5c52-46a5-be6b-6cf904a2b3c3","name":"Some","created":"2014-12-30T18:00:32.349Z","updated":"2014-12-30T18:00:32.349Z","tags":[],"type":"All","parameters":{"Scope":"*","Target":"ALL"}}
That *should ideally* be a json formatted string, but as far as I can see it is just a string. And I'm trying to extract the value of the 'id'. Thus, 7e48b23e-5c52-46a5-be6b-6cf904a2b3c3
The string generated above is random and will be different each time, except the keys that will remain the same
I cannot figure this out. Anybody who can help?

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

Kevin O.

unread,
Jan 2, 2015, 11:10:36 AM1/2/15
to robotframe...@googlegroups.com
Here's two approaches using regex and json module:

JSon
    ${json}=    Set Variable    {"id":"7e48b23e-5c52-46a5-be6b-6cf904a2b3c3","name":"Some","created":"2014-12-30T18:00:32.349Z","updated":"2014-12-30T18:00:32.349Z","tags":[],"type":"All","parameters":{"Scope":"*","Target":"ALL"}}
    ${id1}=    Evaluate    re.search('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}', """${json}""").group(0)    re
    ${blob}=    Evaluate    json.loads("""${json}""")    json
    ${id2}=    Set Variable    ${blob['id']}
    Should Be Equal    ${id1}    ${id2}

komodospider

unread,
Jan 3, 2015, 8:55:42 AM1/3/15
to robotframe...@googlegroups.com
Thank you for your replies. I thought I had deleted this post but apparently I was wrong. I guess I was very close to the solution you mentioned Bill. Here is what I did. I assumed it was json and extracted the id and then removed the "" with get substring.
    ${raw_data}             #raw_data being the entire string
    ${raw_id}      get json value  ${raw_data}   /id     #extracted the id field
    ${id}      get substring   ${raw_id}  1   -1          #to get rid of the quotes around the value

On Wednesday, December 31, 2014 12:11:32 AM UTC+1, Bill Christensen wrote:
I'm a bit rusty on my GREP skills, but in general I'd approach this by selecting all the characters up to the first comma, then remove the first 7 characters (  {"id":" ) and the last character (").  

This of course assumes the ID always comes first in your output string.  

If the order varies you'd have to first find the string that is between "id":" and the next instance of  ",   That should actually be more efficient, as you should be able to do that in one step.  

This also assumes that no other part of the returned string will ever contain the particular string "id":" and that your target string will never contain ",


On Tue, Dec 30, 2014 at 12:13 PM, komodospider <sbk...@gmail.com> wrote:
Hi!

I receive a return value from a RF library like this {"id":"7e48b23e-5c52-46a5-be6b-6cf904a2b3c3","name":"Some","created":"2014-12-30T18:00:32.349Z","updated":"2014-12-30T18:00:32.349Z","tags":[],"type":"All","parameters":{"Scope":"*","Target":"ALL"}}
That *should ideally* be a json formatted string, but as far as I can see it is just a string. And I'm trying to extract the value of the 'id'. Thus, 7e48b23e-5c52-46a5-be6b-6cf904a2b3c3
The string generated above is random and will be different each time, except the keys that will remain the same
I cannot figure this out. Anybody who can help?

--
You received this message because you are subscribed to the Google Groups "robotframework-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to robotframework-users+unsub...@googlegroups.com.

komodospider

unread,
Jan 3, 2015, 8:58:18 AM1/3/15
to robotframe...@googlegroups.com
For to mention, I used the keyword 'get json value' from excellent https://github.com/peritus/robotframework-httplibrary/
Just wanted to add this for anybody reading this as this keyword is not part of the standard (packaged?) RF library
Reply all
Reply to author
Forward
0 new messages