pythonjsonencodingcyrillicanytree

JSON-Encoding for anytree.exporter does not support cyrillic


I've built tree in Python using Anytree (https://pypi.org/project/anytree/2.8.0/). Then I've exported it to JSON. After import tree from file I've got normal symbols. The problem is that JSON-file include 'bad' symbols instead of normal cyrillic. Here is a sample of code:

  1. build tree
  2. export tree to json + render
  3. import tree from json + render
  4. print json content

Code:

import smart_open
import json, pprint
from anytree import AnyNode, RenderTree
from anytree.exporter import JsonExporter
from anytree.importer import JsonImporter
json_file = r'd:\temp\test.json'
#building tree
root = AnyNode(uk = 'узел-0', parent=None)
b = AnyNode(uk = 'узел-01', parent=root)
a = AnyNode(uk = 'узел-011',parent=b)
g = AnyNode(uk = 'узел-021', parent = root)
print()
print('Tree for export:')
print(RenderTree(root).by_attr('uk'))
#export
exporter = JsonExporter(indent=2, sort_keys=True)
data = exporter.export(root)
with open(json_file, 'w', encoding='utf-8') as outfile:
    json.dump(data, outfile, ensure_ascii=False)
#import
importer = JsonImporter()
with open(json_file, encoding='utf-8') as json_file:
    data = json.load(json_file)
root = importer.import_(data)
print()
print('Imported tree:')
print(RenderTree(root).by_attr('uk'))
#print bad json
print()
print('data in JSON-file:')
pprint.pprint(data)

The result is:

> Tree for export: 
> узел-0 
> ├── узел-01 
> │   └── узел-011 
> └── узел-021
> 
> Imported tree:
> узел-0 
> ├── узел-01 
> │   └── узел-011 
> └── узел-021
> 
> data in JSON-file: 
> ('{\n'
>   '  "children": [\n'
>   '    {\n'
>   '      "children": [\n'
>   '        {\n'
>   '          "uk":"\\u0443\\u0437\\u0435\\u043b-011"\n'
>   '        }\n'
>   '      ],\n'
>   '  "uk": "\\u0443\\u0437\\u0435\\u043b-01"\n'
>   '    },\n'
>   '    {\n'
>   '  "uk": "\\u0443\\u0437\\u0435\\u043b-021"\n'
>   '    }\n'
>   '  ],\n'
>   '  "uk": "\\u0443\\u0437\\u0435\\u043b-0"\n'
>   '}')

While writing cyrillic to JSON is ok. Example:

import json, pprint
data = '''
 {
  "uk":"узел-0",
  "children": [
    {
      "uk": "узел-01",
    }
  ]
}'''
json_file = r'd:\temp\test_ru.json'
with open(json_file, 'w', encoding='utf-8') as outfile:
    json.dump(data, outfile, ensure_ascii=False)
with open(json_file, encoding='utf-8') as outfile:
    data = json.load(outfile) 
pprint.pprint(data)

returns:

> ('\n'
>  ' {\n'
>  '  "uk":"узел-0",\n'
>  '  "children": [\n'
>  '    {\n'
>  '      "uk": "узел-01",\n'
>  '    }\n'
>  '  ]\n'
>  '}')

Solution

  • The answer is:

    JsonExporter(indent=2, sort_keys=True, ensure_ascii=False)