5


I'm trying to retrieve a Javascript variable using Python and I'm having some issues...

Here is what the variable looks like :

<script type="text/javascript">
var exampleVar = [
    {...},
    {...},
    {
        "key":"0000",
        "abo":
            {
                "param1":"1"
                "param2":"2"
                "param3":
                    [
                        {
                            "param3a1":"000"
                            "param3a2":"111"
                        },
                        {
                            "param3b1":"100"
                            "param3b2":"101"
                        }
                    ]
             }
]
</script>

After some research, I discovered that its content was in the JSON format, and I'm new to it...

My problem now is that I would like to retrieve the value of "param3b1" (for example) to use it in my Python program.
How do I do this in Python ?
Thanks !

4
  • You could use a module such as docs.python.org/3/library/json.html?highlight=json#module-json
    – matsjoyce
    Oct 7, 2014 at 16:25
  • 1
    Did you consider searching for "Python JSON"? Have a look at json.
    – jonrsharpe
    Oct 7, 2014 at 16:25
  • If the variable is on the client you need to send it back with ajax or a form post. Once it is on the server use json encoder/decoder Oct 7, 2014 at 16:26
  • "After some research, I discovered that it was JSON," No, no, no! JavaScript is not JSON. JSON and JavaScript object literals have very similar syntax (after all, JSON was inspired by that syntax), but that doesn't make them one and the same thing. JavaScript is a programming language, JSON is data format (like XML). Oct 7, 2014 at 17:11

2 Answers 2

4

Step by step this is what you need to do.

  1. extract the json string from the file/html string. you need to get the string between the <script> tags first, and then the variable definition
  2. extract the parameter from the json string.

Here is a demo.

from xml.etree import ElementTree

import json
tree = ElementTree.fromstring(js_String).getroot() #get the root
#use etree.find or whatever to find the text you need in your html file
script_text = tree.text.strip()

#extract json string
#you could use the re module if the string extraction is complex
json_string = script_text.split('var exampleVar =')[1]
#note that this will work only for the example you have given.
try:
    data = json.loads(json_string)
except ValueError:
    print "invalid json", json_string
else:
    value = data['abo']['param3']['param3b1']
2
  • Hi, thanks for this great answer. Do you have a good link on how to use the re module, because I tried to undersand it but I couldn't... Thank you !
    – sylvelk
    Oct 7, 2014 at 17:34
  • @Sek8 You need to understand regex before you can understand the re module. en.wikipedia.org/wiki/Regular_expression . However, if the text you are parsing is simple enough, you should be able to extract the JSON object using split()
    – tom
    Oct 7, 2014 at 17:38
2

You need to use the JSON module.

import json

myJson = json.loads(your_json_string)

param3b1 = myJson['abo']['param3'][1]['param3b1']

JSON module documentation : https://docs.python.org/2/library/json.html

1
  • 1
    Thanks a lot ! My issue now is how to extract my JSON string from the whole <script> and from the var myVar = my_json_string ... Any idea about this ? I think I should use the re module but I'm not at all familiar with it.
    – sylvelk
    Oct 7, 2014 at 17:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.