0

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?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • The backslashes mean this is not valid JSON. Where did you get the file from? Maybe you have some script that's outputting it but incorrectly? – wjandrea Mar 21 '23 at 16:16
  • That's not a correct format of a json file. Your string contains the backslash as an escape character but makes the json content incorrect. Try loading the contents of the file first, remove the backslash and then parse using the json package. – jvieira88 Mar 21 '23 at 16:18
  • 1
    If it's Python that messed up the data, this'll let you parse the backslashes properly: [Process escape sequences in a string in Python](/q/4020539/4518341) – wjandrea Mar 21 '23 at 16:24

2 Answers2

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}]}
wjandrea
  • 28,235
  • 9
  • 60
  • 81
shashank A
  • 34
  • 4
-1

use

import json

with open("4.json", encoding="utf8") as file:
    txt = file.read()
    data = json.loads(txt)
Ilya
  • 93
  • 6