Fix unquoted keys in JSON-like file so that it uses correct JSON syntax

akshitBhatia

I have a very large JSON-like file, but it is not using proper JSON syntax: the object keys are not quoted. I'd like to write a script to fix the file, so that I can load it with json.loads.

I need to match all words followed by a colon and replace them with the quoted word. I think the regex is \w+\s*: and that I should use re.sub, but I'm not exactly sure how to do it.

How can I take the following input and get the given output?

# In
{abc : "xyz", cde : {}, fgh : ["hfz"]}
# Out
{"abc" : "xyz", "cde" : {}, "fgh" : ["hfz"]}

# In
{
    a: "b",
    b: {
        c: "d",
        d: []
    },
    e: "f"
}
# Out
{
    "a": "b",
    "b": {
        "c": "d",
        "d": []
    },
    "e": "f"
}
Zero Piraeus

Rather than a potentially fragile regex solution, you can take advantage of the fact that while your log file isn't valid JSON, it is valid YAML. Using the PyYAML library, you can load it into a Python data structure and then write it back out as valid JSON:

import json
import yaml

with open("original.log") as f:
    data = yaml.load(f)

with open("jsonified.log", "w") as f:
    json.dump(data, f)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to produce JSON with unquoted keys?

Fix JSON keys in PostgreSQL

Parsing unquoted JSON string

Correct syntax for addressing JSON object in Jekyll _data file

Constructing a syntax correct JSON file via BASH while loop

Read a JSON file and generate string keys with values in a dictionary like object

Correct normalizer syntax for normalizing JSON

How to transform unquoted keys in JSON string to quoted keys in R gsub() function

Deserialize JSON with Unquoted Hexadecimal Values

Update and replace unquoted JSON strings

Is there any way to fix package-lock.json lockfileVersion so npm uses a specific format?

How to fix coding keys error with JSON conversion

swift syntax fix for JSON display loop

AWS Cloudformation CLI uses shorthand syntax when passing parameters as json file

Does JSON syntax allow duplicate keys in an object?

Syntax to load nested in nested keys of JSON files

How to fix file .json not found

What is the correct syntax inside the src property in a img tag for a mapped json file using ReactJS?

Deserialize JSON file with different keys

IntelliJ IDEA: json file syntax

Validate the syntax of JSON file in Unix?

what is the right syntax for a json file?

ObjectMapper not printing correct json in file

Validate json file for correct format

Parsing a file that looks like JSON to a JSON

Getting Error: Failed to parse Dockerrun JSON file: json: invalid use of ,string struct tag, trying to unmarshal unquoted value into int

Reading a JSON file uses a lot of memory in node

add keys to a json file, remove duplicates and write to json file in javascript

how to fix file .json not found when it is there