Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How would I write regex to get the json out of this HTML script tag?

So I have this text that I extracted out of a <script> tag.

                                        function fbq_w123456as() {
                                                fbq('track', 'AddToCart', {
                                                contents: [
                                                {
                                                        'id': '123456',
                                                        'quantity': '',
                                                        'item_price':69.99                                                        }
                                                ],
                                                content_name: 'Stackoverflow',
                                                content_category: '',
                                                content_ids: ['w123456as'],
                                                content_type: 'product',
                                                value: 420.69,
                                                currency: 'USD'
                                                });
                                        }

I’m trying to extract this information using regex and later converting it into JSON using python.
I’ve tried re.search(r"'AddToCart', (.*?);" and a few other attempts but no luck. I am very new to regex and I am struggling with it.

{
    "contents":[
        {
            "id":"123456",
            "quantity":"",
            "item_price":69.99
        }
    ],
    "content_name":"Stackoverflow",
    "content_category":"",
    "content_ids":[
        "w123456as"
    ],
    "content_type":"product",
    "value":420.69,
    "currency":"USD"
}

How would I create the regex to extract the JSON data?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can try:

import re
from ast import literal_eval

js_txt = """\
    function fbq_w123456as() {
            fbq('track', 'AddToCart', {
            contents: [
            {
                    'id': '123456',
                    'quantity': '',
                    'item_price':69.99                                                        }
            ],
            content_name: 'Stackoverflow',
            content_category: '',
            content_ids: ['w123456as'],
            content_type: 'product',
            value: 420.69,
            currency: 'USD'
            });
    }"""

out = re.search(r"'AddToCart', (\{.*?\})\);", js_txt, flags=re.S).group(1)
out = re.sub(r"""([^"'\s]+):""", r'"\1":', out)
out = literal_eval(out)
print(out)

Prints python dict:

{
    "contents": [{"id": "123456", "quantity": "", "item_price": 69.99}],
    "content_name": "Stackoverflow",
    "content_category": "",
    "content_ids": ["w123456as"],
    "content_type": "product",
    "value": 420.69,
    "currency": "USD",
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading