0

I have a file with multiple objects like this:

{ 
    name: (sindey, crosby)
    game: "Hockey"
    type: athlete
},
{ 
    name: (wayne, gretzky)
    game: "Ice Hockey"
    type: athlete
}

...and I'd like to convert them to JSON format and output this:

[
    { 
        "name": "(sindey, crosby)",
        "game": "Hockey",
        "type": "athlete"
    },
    { 
        "name": "(wayne, gretzky)",
        "game": "Ice Hockey",
        "type": "athlete"
    }
]

If the input was in this format,

 name: (sidney, crosby) | game:"Hockey" |  type:athlete 
 name: (wayne, gretzky) | game:"Ice Hockey" |  type:athlete

I could implement using json dump with list and dict and it gives me the desired output

import json

f = open("log.file", "r")
content = f.read()
splitcontent = content.splitlines()

d = []
for v in splitcontent:
    l = v.split(' | ')
    d.append(dict(s.split(':',1) for s in l))


with open("json_log.json", 'w') as file:
    file.write((json.dumps(d, indent=4, sort_keys= False)))

How can I reformat this code to convert my input to JSON format?

11
  • The names in the 2 formats are different. Commented Jun 6, 2022 at 12:54
  • @ScottHunter fixed, they're both meant to be in the same format. Commented Jun 6, 2022 at 13:02
  • 1
    Let's be clear: you have a non-standard file format and you have to write a parser for it. You wrote one for your other non-standard format; what is stopping your from writing one for your first one? Commented Jun 6, 2022 at 13:11
  • 2
    Please show what you have tried & what specific issues you are having with it. Commented Jun 6, 2022 at 13:15
  • 1
    @SarahMesser: Doesn't splitlines already handle that? Commented Jun 6, 2022 at 13:42

2 Answers 2

2

With slight changes on the answer given by @sarah Messer. Changes involved

lines without the : separator skipped

Try this

import json


f = open("log.file", "r")
content = f.read()
splitcontent = content.splitlines()

d = []
appendage = {}
for line in splitcontent:

    if ('}' in line) or ('{' in line) or ('{' in line) or ('}' in line):
        # Append a just-created record and start a new one
        if appendage:
            d.append(appendage)
            appendage = {}
        continue

    key, val = line.split(':')

    if val.endswith(','):
        # strip a trailing comma
        val = val[:-1]
        print(val)
    # if val == "":
    #     pass
    # else:
    appendage[key] = val


with open("json_log.json", 'w') as file:
    file.write((json.dumps(d, indent=4, sort_keys=False)))
Sign up to request clarification or add additional context in comments.

2 Comments

any idea why it's outputting only the 2nd object 6 times. The output is in the format I needed but outputs the (wayne, gretzky).... object 6 times
@learner22 checkout the updated answer.
2

Something like this will probably work for most cases - you just have to handle the lines with curly braces separately from the lines with data:

import json

f = open("log.file", "r")
content = f.read()
splitcontent = content.splitlines()

d = []
appendage = {}
for line in splitcontent:
    if ('}' in line) or ('{' in line):
        # Append a just-created record and start a new one
        if appendage:
            d.append(appendage)
        appendage ={}
    else:
        key, val = line.split(':',1)
        if val.endswith(','):
            # strip a trailing comma
            val = val[:-1]
        appendage[key] = val


with open("json_log.json", 'w') as file:
    file.write((json.dumps(d, indent=4, sort_keys= False)))

I might also have some typos in there...

Comments

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.