0

I know this Question is already answered, but I dont know where the Error is in my Case.

This is my Code:

import json

json_data = """
{
    'position1': '516, 440',
    'position2': '971, 443',
    'position3': '1186, 439',
    'position4': '1402, 441',
    'position5': '1630, 449',
    'position6': '299, 681',
    'position7': '518, 684',
    'position8': '736, 691',
    'position9': '739, 431'
}
"""
data = json.loads(json_data)
print(data)

Im not really into working with json files, so please don't blame me if it's a really dump mistake.

2
  • 1
    The error message seems pretty clear - what don’t you understand about it? Commented May 28, 2021 at 22:20
  • 1
    '516, 440', the problem is the comma in the middle of the numbers is being interpreted the same as the comma at the end of the key:value pair, so you need to use double quotes instead of single quotes if you want commas in your values Commented May 28, 2021 at 22:22

3 Answers 3

3

Don't use triple quotes """. Instead use a dictionary with json.dumps() so that commas in your values are not misinterpreted as commas between items.

import json

json_data = {
    'position1': '516, 440',
    'position2': '971, 443',
    'position3': '1186, 439',
    'position4': '1402, 441',
    'position5': '1630, 449',
    'position6': '299, 681',
    'position7': '518, 684',
    'position8': '736, 691',
    'position9': '739, 431'
}

data = json.dumps(json_data)
print(data)
Sign up to request clarification or add additional context in comments.

1 Comment

Worked! Thank you!
3

If you are using triple quotes, this would work

json_data = json_data.replace("'", '"')

data = json.loads(json_data)
print(data)

1 Comment

I was using this and worked fine until I had a value containing a '
0

Try this one

import json

json_data = {
    'position1': '516, 440',
    'position2': '971, 443',
    'position3': '1186, 439',
    'position4': '1402, 441',
    'position5': '1630, 449',
    'position6': '299, 681',
    'position7': '518, 684',
    'position8': '736, 691',
    'position9': '739, 431'
}

data = json.dumps(json_data)
print(data)

1 Comment

This is the exact same code as in my answer.

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.