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)

How to Escape Double Quote inside JSON

Asked By: mssqlsense

||

Answers:

Using Regex:

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

Output:

{ "Name": "test", "Address": "xyz"}
Answered By: Rakesh

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.

Answered By: mssqlsense

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)
    }
}
Answered By: Big Pumpkin

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

Answered By: Jay Shukla

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)

Answered By: hectorcanto

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

Answered By: Manoranjan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.