0

I'm quite new to python and currently am working on some string manipulation to generate a JSON schema. I've a list with objects and a string with these objects nested in a parent-child hierarchy. So I've a list and a string which is passed to parser function:

objectList = ['Object A', 'Object B', 'Object C']
objectString = '{"Object A": {"Object B": {"Object C": {}}}}'

The function is as follows:

#parsing properties to json format
def transform_toJson(objects, old_format):
    obj = objects
    new_format = old_format
    for o in obj:
        position = new_format.find(str(o))
        if position == -1:
            continue
        position += len(o)
        new_format = new_format[:position+1] + ' "type": "object","properties": ' + new_format[position:]
    return new_format


#transform to json format
jsonFormat = transform_toJson(objectList, objectString)

I get the following output:

{"Object A" "type": "object","properties": ": 
{"Object B" "type": "object","properties": ": 
{"Object C" "type": "object","properties": ": {}}}}

The double quotes at the end of each line seem to be added and I don't see what's going wrong.

martineau
  • 112,593
  • 23
  • 157
  • 280
Sef
  • 49
  • 6

0 Answers0