Python Forum
Parsing JSON with backslashes
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Parsing JSON with backslashes
#1
I have a script where the users will need to edit some variables. I don't want them to mess the script up so decided I would make a config json file where they could just edit the json rather than edit the script.

I don't want the users to have to understand \\, I want them to fill it in the way you would normally type a file path or domain user account. The problem is I get a json.decoder.JSONDecodeError: Invalid \uXXXX escape: line 5 column 22 (char 243) error. I understand why this is, but is there a way I can make it easy for the user, and Python ignore the error. Once I have parsed the JSON I will sort out the paths so Python can understand them.

def read_json_config():
    with open('console_config.json') as json_data:
        json_config_file = json.load(json_data)
This is my JSON file.

{
"ClientID":"8477295f-405",
"ClientSecret":"10f35a425670847eb63d9b1954592d2b8305cd87e3",
"ReportName":"Unprotected_Machines_",
"ReportFilePath":"c:\users\accounts\desktop\reports\",
"ConsoleName":"UK PS",
"SearchDomain":"dc=domain,dc=co,dc=uk",
"SearchUser":"domain\dap.l",
"SearchUserPassword":"password",
"DomainController":"10.0.1.20"
}
Reply
#2
You might be able to achieve that with a custom parser but I really, really wouldn't recommend that. If a regular Python file doesn't work for you, I'd recommend you give this a look.
Reply
#3
Two errors in this path because of escape characters.
>>> s = "c:\users\accounts\desktop\reports" # Removed last \
  File "<interactive input>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \uXXXX escape

>>> # Fix 
>>> s = "c:\\users\\accounts\\desktop\\reports\\"
>>> s
'c:\\users\\accounts\\desktop\\reports\\'
>>> 
Do no use single \ in file paths.
import json
from pprint import pprint

def read_json_config():
    with open('c.json') as json_data:
        json_config_file = json.load(json_data)
        pprint(json_config_file)

read_json_config()
Output:
{'ClientID': '8477295f-405', 'ClientSecret': '10f35a425670847eb63d9b1954592d2b8305cd87e3', 'ConsoleName': 'UK PS', 'DomainController': '10.0.1.20', 'ReportFilePath': 'c:\\users\\accounts\\desktop\\reports', 'ReportName': 'Unprotected_Machines_', 'SearchDomain': 'dc=domain,dc=co,dc=uk', 'SearchUser': 'domain\\dap.l', 'SearchUserPassword': 'password'}
Reply
#4
(Jan-24-2020, 06:21 PM)micseydel Wrote: You might be able to achieve that with a custom parser but I really, really wouldn't recommend that. If a regular Python file doesn't work for you, I'd recommend you give this a look.

This worked a treat. I have converted all my scripts that needed this option. They work perfectly. Thank you very much.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Parsing large JSON josvink66 5 624 Jan-10-2024, 05:46 PM
Last Post: snippsat
  Very simple question about filenames and backslashes! garynewport 4 1,919 Jan-17-2023, 05:02 AM
Last Post: deanhystad
  Parsing JSON pyStund 4 2,962 Jul-31-2022, 02:02 PM
Last Post: pyStund
  Json Parsing sshree43 5 1,779 May-04-2022, 09:21 PM
Last Post: snippsat
  json api data parsing elvis 0 922 Apr-21-2022, 11:59 PM
Last Post: elvis
  string indices must be integers when parsing Json ilknurg 3 6,334 Mar-10-2022, 11:02 AM
Last Post: DeaD_EyE
  Help Parsing JSON kfwydfo1x 5 4,568 Jan-26-2021, 10:42 AM
Last Post: DeaD_EyE
  JSON parsing (nested) fakka 0 3,063 Nov-25-2019, 09:25 PM
Last Post: fakka
  f-strings and backslashes newbieAuggie2019 5 3,714 Oct-26-2019, 06:58 AM
Last Post: buran
  Parsing json - dictionary with in dictionary krish216 6 3,626 Jul-30-2019, 10:01 PM
Last Post: cvsae

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020