6

I'm facing some difficulties while trying to figure it out, what it the optimal way to convert a string to a valid JSON, using Python.

Basically, what I need to do is to read a string (which is already given in a JSON format) from a file, and convert it to a valid JSON. The only problem in my case, is the " , " after the last object:

    {
        "InputTable" : 
        [
            {
                "ServerName":"serverOne",
                "To":"userOne", 
                "CC":"", 
                "TemplateName":"LinuxTemplate" 
            },
            {
                "ServerName":"serverTwo",
                "To":"userTwo", 
                "CC":"userFive", 
                "TemplateName":"LinuxTemplateWithCC" 

            }, << get rid of this comma

        ],
        "Params": 
        [   

            {"Col_0":"Server","Col_1":"User","Col_2":"Action"}
        ]
    }

This is how I read the file:

with open('/nfs/somePath/LinuxInput.JSON') as json_file:  
        try:
            jsonFormatInputTable = json.load(json_file)
        except:
            print ("Couldn't read JSON file, cancled operation.")
            return

Loading the JSON file fails because the JSON is not valid. Do you have any suggestion, first how to get rid of that last comma, and second how to validate the string and make sure it is in the correct format?

4
  • 5
    Why isn't it valid JSON to begin with? If this is a one-time thing… just delete it manually. If some other program is producing that "JSON", it needs to be fixed to produce valid JSON. Commented Jun 14, 2019 at 6:36
  • 2
    Unless you want to write a complicated regex to parse it, better fix the problem at source where the json is produced Commented Jun 14, 2019 at 6:39
  • try parsing using ast.literal_eval() Commented Jun 14, 2019 at 6:44
  • @DeveshKumarSingh, it's not that complicated: ru.stackoverflow.com/a/1392736/178988 Commented Mar 22, 2022 at 23:42

4 Answers 4

12

You could parse it as YAML (whose in-line syntax is a more permissive superset of JSON):

import yaml
data = yaml.load(open(json_file))

then to get valid JSON back you can dump the object back out:

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

3 Comments

Now that is a hack if I've ever seen one.
@deceze it seems like a pretty robust solution to me. valid json is still handled correctly (this is a requirement for yaml) and invalid json is handled better.
Great idea. I was tempted to use eval() but that has security considerations when you're receiving rando data. :)
3

If your JSON is valid except for the trailing commas, you can try using more relaxed parsers like the other solution said.

If you just want to remove the trailing commas, this regex should work.

(?<=[}\]"']),(?!\s*[{["'])

Replace all occurrences with an empty string.

import re
regex = r'''(?<=[}\]"']),(?!\s*[{["'])'''
result = re.sub(regex, "", test_str, 0)

1 Comment

This replacement can damage strings. ru.stackoverflow.com/a/1392736/178988
2

You could using jsoncomment https://pypi.python.org/pypi/jsoncomment

import json
from jsoncomment import JsonComment

with open('/nfs/somePath/LinuxInput.JSON') as json_file:    
    parser = JsonComment(json)
    data = parser.load(json_file)

Comments

-1

There You go:

=^..^=

test_string = """{
    "InputTable" : 
    [
        {
            "ServerName":"serverOne",
            "To":"userOne", 
            "CC":"", 
            "TemplateName":"LinuxTemplate" 
        },
        {
            "ServerName":"serverTwo",
            "To":"userTwo", 
            "CC":"userFive", 
            "TemplateName":"LinuxTemplateWithCC" 

        },

    ],
    "Params": 
    [   

        {"Col_0":"Server","Col_1":"User","Col_2":"Action"}
    ]
}"""

test_string = re.sub("},\n\n", "}\n", test_string)
valid_jonson = json.loads(test_string)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.