1

I want to get the output with single backslash in my json file which is an input to my javascript/HTML document. it is for visualization purposes using vis.js I am unable to get rid of Python's way of adding a backslash before a backslash. I have tried escaping backslash and using r'' type strings.

I have a dictionary:

f = {'id':0,'group':'main', 'label':'Main', \
'font':{'size': 60}, 'shape': 'icon', 'icon': {'face':'Ionicons',\
'code': r'\uf276', 'size':200, 'color':'#f0a30a'}, \
'color': {'background': 'pink', 'border': 'black'}}

In [91]: print f
'{'group': 'main', 'color': {'border': 'black', 'background': 'pink'}, 'label': 'Main', 'shape': 'icon', 'font': {'size': 60}, 'id': 0, 'icon': {'size': 200, 'color': '#f0a30a', 'code': '\\uf276', 'face': 'Ionicons'}}

I am dumping it into a json file using json.dumps

In [92]: json.dumps(f)
Out[92]: '{"group": "main", "color": {"border": "black", "background": "pink"}, "label": "Main", "shape": "icon", "font": {"size": 60}, "id": 0, "icon": {"size": 200, "color": "#f0a30a", "code": "\\\\uf276", "face": "Ionicons"}}'

In [93]: print json.dumps(f)
{"group": "main", "color": {"border": "black", "background": "pink"}, "label": "Main", "shape": "icon", "font": {"size": 60}, "id": 0, "icon": {"size": 200, "color": "#f0a30a", "code": "\\uf276", "face": "Ionicons"}}

I am saving this string into a file using json.dump(filename, f) and it is outputting this:

{
    "group": "main", 
    "color": {
        "border": "black", 
        "background": "pink"
    }, 
    "label": "Main", 
    "shape": "icon", 
    "font": {
        "size": 60
    }, 
    "id": 0, 
    "icon": {
        "size": 200, 
        "color": "#f0a30a", 
        "code": "\\uf276", 
        "face": "Ionicons"
    }
} 
1
  • Your backslash is in a raw string (r'foobar'), which is why it's actually a backslash, not an escape sequence. Commented Mar 11, 2016 at 21:51

2 Answers 2

2
json.dumps(f, ensure_ascii=False).encode('utf8')

will give you

{
    "group": "main",
    "color": {
        "border": "black",
        "background": "pink"
    },
    "label": "Main",
    "shape": "icon",
    "font": {
        "size": 60
    },
    "id": 0,
    "icon": {
        "size": 200,
        "color": "#f0a30a",
        "code": "\xef\x89\xb6",
        "face": "Ionicons"
    }
}

EDIT Also the code should be u'\uf276'

Sign up to request clarification or add additional context in comments.

Comments

0

The string containing the backslash is a raw string. It doesn't contain the unicode character you want, but instead an actual backslash, followed by "uf276".

Turn it into a normal string:

f = {'id':0,'group':'main', 'label':'Main', \
'font':{'size': 60}, 'shape': 'icon', 'icon': {'face':'Ionicons',\
'code': '\uf276', 'size':200, 'color':'#f0a30a'}, \
'color': {'background': 'pink', 'border': 'black'}}

Then the json-dumping should work, too:

>>> print(json.dumps(f, indent=4))
{
    "shape": "icon",
    "font": {
        "size": 60
    },
    "label": "Main",
    "color": {
        "background": "pink",
        "border": "black"
    },
    "group": "main",
    "id": 0,
    "icon": {
        "face": "Ionicons",
        "color": "#f0a30a",
        "size": 200,
        "code": "\uf276"
    }
}

3 Comments

it is not working even then. I had tried normal strings too. When I save it using json.dumps(), it just adds an extra backslash before that.
@mousecoder When you're viewing the resulting string of json.dumps in your REPL, of course. But when you print it (or dump it to a file), it's fine, see edit.
i think the problem is that when you print it, it does not print double backslash but if you save it in a json file the strings appear with double backslash. Can you check that please?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.