I need to grab the values of a JSON string stored within a function in a script tag like this:
<script type="text/javascript">
my.function("bar", {"foo1": false, "foo2": true, "foo3": "foobar!"});
</script>
I can get to the particular tag using Mechanize like so:
parser.xpath("//script[ contains(text(), 'my.function')]").text
but I’m not sure how to go on from there. How can I extract the JSON part of the string and convert it into a hash so that I can extract the values?
If the form doesn’t change, you can do
with
jsongem. Be aware that there are several points of failure – either check each step, or place a nicerescuesomewhere.