skip to Main Content

I have a json file and data is like this

{"Status":"OK","isSuccess":true,"Error":"","Data":[{"STOK_KODU":"REMA-50100","BR1_PAYDA":1,"MARKA":"REMARK","URETICI_KODU":"50100","KDV_ORANI":18.0,"STOK_DURUM":"3ARA","IMG":1,"STOK_TEXT":"REM03@FAN MOTORU 24VANELI  7\"@@ REMARK@ @@ XRFM1103@ 009 2985@ 2394009@ RF03","ORAN1":43.00000000,"ORAN2":7.00000000,"ORAN3":0.00000000,"ORAN4":0.0,"ORAN5":0.0,"ORAN6":0.0,"ORAN7":0.0,"SATIS1":9067.9},{"STOK_KODU":"REMA-03","BR1_PAYDA":1,"MARKA":"REMARK","URETICI_KODU":"503","KDV_ORANI":18.0,"STOK_DURUM":"3ARA","IMG":1,"STOK_TEXT":"R103@MARS MOTORU 24V75 KW DELC955 D STR3@@ REMARK@ @TYERA/BR23/ @ XRE0103@ @ 2313@ 50103","ORAN1":43.00000000,"ORAN2":7.00000000,"ORAN3":0.00000000,"ORAN4":0.0,"ORAN5":0.0,"ORAN6":0.0,"ORAN7":0.0,"SATIS1":9067.9}]}

as you can see, there are some problems with backslash

I tried to read it like this

import json

with open("4.json", encoding="utf8") as file:
    reader = json.load(file)
    print(reader["Status"])

but I am getting error because of the backslash so my question is, is there way to read this file and access the data in it?

2

Answers


  1. you can do

    import json
    
    with open("4.json", encoding="utf8") as file:
        data = file.read()
        reader = json.loads(data.replace('\"', '"').replace('\\"', '\"'))
    # while writing a json file use json.dump(file_object, reader)
    

    Result:

    {'Status': 'OK',
     'isSuccess': True,
     'Error': '',
     'Data': [{'STOK_KODU': 'REMA-50100',
               'BR1_PAYDA': 1,
               'MARKA': 'REMARK',
               'URETICI_KODU': '50100',
               'KDV_ORANI': 18.0,
               'STOK_DURUM': '3ARA',
               'IMG': 1,
               'STOK_TEXT': 'REM03@FAN MOTORU 24VANELI  7"@@ REMARK@ @@ XRFM1103@ '
                            '009 2985@ 2394009@ RF03',
               'ORAN1': 43.0,
               'ORAN2': 7.0,
               'ORAN3': 0.0,
               'ORAN4': 0.0,
               'ORAN5': 0.0,
               'ORAN6': 0.0,
               'ORAN7': 0.0,
               'SATIS1': 9067.9},
              {'STOK_KODU': 'REMA-03',
               'BR1_PAYDA': 1,
               'MARKA': 'REMARK',
               'URETICI_KODU': '503',
               'KDV_ORANI': 18.0,
               'STOK_DURUM': '3ARA',
               'IMG': 1,
               'STOK_TEXT': 'R103@MARS MOTORU 24V75 KW DELC955 D STR3@@ REMARK@ '
                            '@TYERA/BR23/ @ XRE0103@ @ 2313@ 50103',
               'ORAN1': 43.0,
               'ORAN2': 7.0,
               'ORAN3': 0.0,
               'ORAN4': 0.0,
               'ORAN5': 0.0,
               'ORAN6': 0.0,
               'ORAN7': 0.0,
               'SATIS1': 9067.9}]}
    
    Login or Signup to reply.
  2. use

    import json
    
    with open("4.json", encoding="utf8") as file:
        txt = file.read()
        data = json.loads(txt)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search