While trying to create YAML from a JSON in python, using the PyYAML library, I am able to convert the JSON into YAML. However, in the YAML I receive as a result, all the brackets of JSON are unfolded whereas I want to retain few square brackets (lists) from JSON to converted YAML. How can I request this library call to not unfold lists from JSON into YAML, but rather retain it as a list?
A snapshot of my issue follows:
import yaml
import json
original_json = {'a': {'next': ['b'], 'prev': []},
'b': {'next': ['c'], 'prev': ['a']},
'c': {'next': ['d', 'e'], 'prev': ['b']},
'd': {'next': [], 'prev': ['c']},
'e': {'next': ['f'], 'prev': ['c']},
'f': {'next': [], 'prev': ['e']}}
obtained_yaml = yaml.dump(yaml.load(json.dumps(original_json)), default_flow_style=False)
# obtained_yaml looks like
#
# a:
# next:
# - b
# prev: []
# b:
# next:
# - c
# prev:
# - a
# c:
# next:
# - d
# - e
# prev:
# - b
# d:
# next: []
# prev:
# - c
# e:
# next:
# - f
# prev:
# - c
# f:
# next: []
# prev:
# - e
# expected yaml should look like
#
# a:
# next:
# - ["b"]
# prev: []
# b:
# next:
# - ["c"]
# prev:
# - ["a"]
# c:
# next:
# - ["d"]
# - ["e"]
# prev:
# - ["b"]
# d:
# next: []
# prev:
# - ["c"]
# e:
# next:
# - ["f"]
# prev:
# - ["c"]
# f:
# next: []
# prev:
# - ["e"]
I tried few ways to solve this out but all that did not work in the way expected json should come out. Need suggestions on how to get it done.