5
votes

I currently have JSON in the below format. Some of the Key values are NOT properly formatted as they are missing double quotes (")

How do I fix these key values to have double-quotes on them?

    {      
Name: "test",
Address: "xyz",
"Age": 40,
"Info": "test"
}

Required:

    {      
"Name": "test",
"Address": "xyz",
"Age": 40,
"Info": "test"
}

Using the below post, I was able to find such key values in the above INVALID JSON. However, I could NOT find an efficient way to replace these found values with double-quotes.

s = "Example: String"
out = re.findall(r'\w+:', s)

How to Escape Double Quote inside JSON

5
are the keys at the start of the line, everytime?Jean-François Fabre♦
No Jean, a few of the keys are hidden in between. I do NOT have access to change source format. Hence, have to process it at my end.mssqlsense

5 Answers

9
votes

Using Regex:

import re
data = """{ Name: "test", Address: "xyz"}"""
print( re.sub("(\w+):", r'"\1":',  data) )

Output:

{ "Name": "test", "Address": "xyz"}
3
votes

I had few more issues that I faced in my JSON. Thought of sharing the final solution that worked for me.

jsonStr = re.sub("((?=\D)\w+):", r'"\1":',  jsonStr)
jsonStr = re.sub(": ((?=\D)\w+)", r':"\1"',  jsonStr)
  1. First Line will fix this double-quotes issue for the Key. i.e. Name: "test"
  2. Second Line will fix double-quotes issue for the value. i.e. "Info": test

Also, above will exclude double-quoting within date timestamp which have : (colon) in them.

1
votes

You can use online formatter. I know most of them are throwing error for not having double quotes but below one seems handling it nicely!

JSON Formatter

1
votes

You can use PyYaml. Since JSON is a subset of Yaml, pyyaml may overcome the lack of quotes.

Example

import yaml

dirty_json = """
     {
  key: "value",
  "key2": "value"
}
"""
yaml.load(dirty_json, yaml.SafeLoader)

0
votes

The regex approach can be brittle. I suggest you find a library that can parse the JSON text that is missing quotes.

For example, in Kotlin 1.4, the standard way to parse a JSON string is using Json.decodeFromString. However, you can use Json { isLenient = true }.decodeFromString to relax the requirements for quotes. Here is a complete example in JUnit.

import kotlinx.serialization.Serializable
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.json.Json
import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test

@Serializable
data class Widget(val x: Int, val y: String)

class JsonTest {

    @Test
    fun `Parsing Json`() {
        val w: Widget = Json.decodeFromString("""{"x":123, "y":"abc"}""")
        Assertions.assertEquals(123, w.x)
        Assertions.assertEquals("abc", w.y)
    }

    @Test
    fun `Parsing Json missing quotes`() {
        // Json.decodeFromString("{x:123, y:abc}") failed to decode due to missing quotes
        val w: Widget = Json { isLenient = true }.decodeFromString("{x:123, y:abc}")
        Assertions.assertEquals(123, w.x)
        Assertions.assertEquals("abc", w.y)
    }
}