Reading a directory from a JSON file in Python

2900 views json
-3

Trying to read data from a JSON file that contains a directory but unfortunately the directory has escape characters in it.

see below my code and JSON file.

config.json

{
    "src": "C:\Users\tom",
    "dst": "output"
}

run.py

import os
import json

# Set config
with open('config.json') as json_data_file:
    data = json.load(json_data_file)
    src = data["src"]
    dst = data["dst"]

The error that I am receiving follows:

json.decoder.JSONDecodeError: Expecting value: line 2 column 11 (char 15)

I have re-written the code for this question so the values above are incorrect.

answered question

1 Answer

0

JSON requires you to escape backslashes, so simply replace every \ with \\ in config.json.

posted this

Have an answer?

JD

Please login first before posting an answer.