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

convert data in json format

import enum
import requests
from bs4 import BeautifulSoup
import json
import pandas as pd 
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.3"
}
r = requests.get("https://www.fleetpride.com/parts/otr-brake-drum-otr1601b")
soup = BeautifulSoup(r.content, "html5lib")
raw_json = ""
for table_index,table in enumerate( soup.find_all("script")):
    if('CCRZ.detailData.jsonProductData = {"' in str(table)):
        x=str(table).split('CCRZ.detailData.jsonProductData = {"')
        raw_json = "{\""+str(x[-1]).split('};')[0]+"}"
        break
      
      
req_json = json.loads(raw_json)
# uri = req_json.keys()

print(req_json)

this is my ouput show when I run code :

enter image description here

I want like theses in json format can it will be possible to convert in this format if possible can you tell how we convert in json form like these as you shown below I am grateful to you:

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

{
    "mediaWrappers": {
        "Alternate Images": [{
                "uri": "https://www.fleetpride.com/imagesns/PDPF/OTR1601B-Webb copy.jpg",
                "mediaName": "https://www.fleetpride.com/imagesns/PDPF/OTR1601B-Webb copy.jpg",
                "sourceType": "URI",
                "URI": "https://www.fleetpride.com/imagesns/PDPF/OTR1601B-Webb copy.jpg",
                "startDate": "2018-05-23",
                "sequence": 2,
                "productMediaId": "OTR-OTR1601B-Alternate Images-2",
                "mediaType": "Alternate Images",
                "locale": "en_US",
                "endDate": "2099-12-31",
                "enabled": true,
                "altMessage": "OTR OTR OTR1601B",
                "sfdcName": "406524",
                "sfid": "a8B1W000000c4a1UAA",
                "product": "a8G1W000000Y7DfUAK"
            },
            

>Solution :

Both outputs are JSON formats. What you are looking for is called pretty printing. Read about json.dumps() and its options. You want to use the indent option.

...
pretty = json.dumps(req_json, indent=4)
print(pretty)
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