How to Fix JSON Key Values without double-quotes?
Question:
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)
Answers:
Using Regex:
import re
data = """{ Name: "test", Address: "xyz"}"""
print( re.sub("(w+):", r'"1":', data) )
Output:
{ "Name": "test", "Address": "xyz"}
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)
- First Line will fix this double-quotes issue for the Key. i.e.
Name: “test”
- 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.
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)
}
}
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!
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)
I know this is an old question, but if somebody needs to convert the unquoted json to quoted json they can use YAML to JSON converters like this YAML to JSON converter
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)
Using Regex:
import re
data = """{ Name: "test", Address: "xyz"}"""
print( re.sub("(w+):", r'"1":', data) )
Output:
{ "Name": "test", "Address": "xyz"}
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)
- First Line will fix this double-quotes issue for the Key. i.e.
Name: “test” - 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.
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)
}
}
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!
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)
I know this is an old question, but if somebody needs to convert the unquoted json to quoted json they can use YAML to JSON converters like this YAML to JSON converter