skip to Main Content

I accidentally saved a JSON-like string in my database that uses single quotes instead of double quotes, and JSON formatters don’t recognize it as valid JSON. Here is the string that I have:

import json

json_string = """
{'author': 'Hebbars Kitchen', 'canonical_url': 'https://hebbarskitchen.com/til-chikki-recipe-sesame-chikki-gajak/', 'category': 'sweet', 'cook_time': 10, 'cuisine': 'Indian', 'description': 'easy til chikki recipe | sesame chikki recipe | til ki chikki or til gajak', 'host': 'hebbarskitchen.com', 'image': 'https://hebbarskitchen.com/wp-content/uploads/mainPhotos/til-chikki-recipe-sesame-chikki-recipe-til-ki-chikki-or-til-gajak-2.jpeg', 'ingredient_groups': [{'ingredients': ['1 cup sesame / til (white)', '1 tsp ghee / clarified butter', '1 cup jaggery / gud'], 'purpose': None}], 'ingredients': ['1 cup sesame / til (white)', '1 tsp ghee / clarified butter', '1 cup jaggery / gud'], 'instructions': 'firstly in a pan dry roast 1 cup sesame on low flame till it splutters.nnow in another kadai heat 1 tsp ghee and add 1 cup jaggery.nkeep stirring on medium flame till the jaggery melts completely. alternatively, use sugar, if you do not prefer jaggery.nboil the jaggery syrup on low flame till the syrup turns glossy and thickens.ncheck the consistency, by dropping syrup into a bowl of water, it should form hard ball and cut with a snap sound. else boil for another minute and check.nsimmer the flame add add roasted sesame seeds.nstir well making sure jaggery syrup coats well.nimmediately pour the mixture over butter paper or onto steel plate greased with ghee. be quick else the mixture turns hard and will be difficult to set.nget together forming a block, be careful as the mixture will be very hot.nnow using a rolling pin roll the slightly thick block.nallow to cool for a minute, and when its still warm cut into pieces.nlastly, serve til chikki once cooled completely, or store in a airtight container and serve for a month.', 'instructions_list': ['firstly in a pan dry roast 1 cup sesame on low flame till it splutters.', 'now in another kadai heat 1 tsp ghee and add 1 cup jaggery.', 'keep stirring on medium flame till the jaggery melts completely. alternatively, use sugar, if you do not prefer jaggery.', 'boil the jaggery syrup on low flame till the syrup turns glossy and thickens.', 'check the consistency, by dropping syrup into a bowl of water, it should form hard ball and cut with a snap sound. else boil for another minute and check.', 'simmer the flame add add roasted sesame seeds.', 'stir well making sure jaggery syrup coats well.', 'immediately pour the mixture over butter paper or onto steel plate greased with ghee. be quick else the mixture turns hard and will be difficult to set.', 'get together forming a block, be careful as the mixture will be very hot.', 'now using a rolling pin roll the slightly thick block.', 'allow to cool for a minute, and when its still warm cut into pieces.', 'lastly, serve til chikki once cooled completely, or store in a airtight container and serve for a month.'], 'language': 'en-US', 'nutrients': {}, 'prep_time': 5, 'ratings': 5.0, 'ratings_count': 196, 'site_name': "Hebbar's Kitchen", 'title': 'til chikki recipe | sesame chikki recipe | til ki chikki or til gajak', 'total_time': 15, 'yields': '24 servings'}
"""

formatted_json = json.dumps(json_string)

print(formatted_json)

The string contains single quotes instead of double quotes, making it invalid JSON. I tried using json.dumps() to format it, but this just converts the string into another JSON string rather than fixing the issue.

I also tried using ast.literal_eval and demjson3, nothing seems to be working.

Using ast.literal_eval()

import json

# Assume your JSON string is stored in a variable called 'json_string'
json_string = """
{'author': 'Hebbars Kitchen', 'canonical_url': 'https://hebbarskitchen.com/til-chikki-recipe-sesame-chikki-gajak/', 'category': 'sweet', 'cook_time': 10, 'cuisine': 'Indian', 'description': 'easy til chikki recipe | sesame chikki recipe | til ki chikki or til gajak', 'host': 'hebbarskitchen.com', 'image': 'https://hebbarskitchen.com/wp-content/uploads/mainPhotos/til-chikki-recipe-sesame-chikki-recipe-til-ki-chikki-or-til-gajak-2.jpeg', 'ingredient_groups': [{'ingredients': ['1 cup sesame / til (white)', '1 tsp ghee / clarified butter', '1 cup jaggery / gud'], 'purpose': None}], 'ingredients': ['1 cup sesame / til (white)', '1 tsp ghee / clarified butter', '1 cup jaggery / gud'], 'instructions': 'firstly in a pan dry roast 1 cup sesame on low flame till it splutters.nnow in another kadai heat 1 tsp ghee and add 1 cup jaggery.nkeep stirring on medium flame till the jaggery melts completely. alternatively, use sugar, if you do not prefer jaggery.nboil the jaggery syrup on low flame till the syrup turns glossy and thickens.ncheck the consistency, by dropping syrup into a bowl of water, it should form hard ball and cut with a snap sound. else boil for another minute and check.nsimmer the flame add add roasted sesame seeds.nstir well making sure jaggery syrup coats well.nimmediately pour the mixture over butter paper or onto steel plate greased with ghee. be quick else the mixture turns hard and will be difficult to set.nget together forming a block, be careful as the mixture will be very hot.nnow using a rolling pin roll the slightly thick block.nallow to cool for a minute, and when its still warm cut into pieces.nlastly, serve til chikki once cooled completely, or store in a airtight container and serve for a month.', 'instructions_list': ['firstly in a pan dry roast 1 cup sesame on low flame till it splutters.', 'now in another kadai heat 1 tsp ghee and add 1 cup jaggery.', 'keep stirring on medium flame till the jaggery melts completely. alternatively, use sugar, if you do not prefer jaggery.', 'boil the jaggery syrup on low flame till the syrup turns glossy and thickens.', 'check the consistency, by dropping syrup into a bowl of water, it should form hard ball and cut with a snap sound. else boil for another minute and check.', 'simmer the flame add add roasted sesame seeds.', 'stir well making sure jaggery syrup coats well.', 'immediately pour the mixture over butter paper or onto steel plate greased with ghee. be quick else the mixture turns hard and will be difficult to set.', 'get together forming a block, be careful as the mixture will be very hot.', 'now using a rolling pin roll the slightly thick block.', 'allow to cool for a minute, and when its still warm cut into pieces.', 'lastly, serve til chikki once cooled completely, or store in a airtight container and serve for a month.'], 'language': 'en-US', 'nutrients': {}, 'prep_time': 5, 'ratings': 5.0, 'ratings_count': 196, 'site_name': "Hebbar's Kitchen", 'title': 'til chikki recipe | sesame chikki recipe | til ki chikki or til gajak', 'total_time': 15, 'yields': '24 servings'}
"""

# Dump the JSON with proper formatting
ast.literal_eval(json_string)

print(formatted_json)

Error i am getting

  File <unknown>:2
    {'author': 'Hebbars Kitchen', 'canonical_url': 'https://hebbarskitchen.com/til-chikki-recipe-sesame-chikki-gajak/', 'category': 'sweet', 'cook_time': 10, 'cuisine': 'Indian', 'description': 'easy til chikki recipe | sesame chikki recipe | til ki chikki or til gajak', 'host': 'hebbarskitchen.com', 'image': 'https://hebbarskitchen.com/wp-content/uploads/mainPhotos/til-chikki-recipe-sesame-chikki-recipe-til-ki-chikki-or-til-gajak-2.jpeg', 'ingredient_groups': [{'ingredients': ['1 cup sesame / til (white)', '1 tsp ghee / clarified butter', '1 cup jaggery / gud'], 'purpose': None}], 'ingredients': ['1 cup sesame / til (white)', '1 tsp ghee / clarified butter', '1 cup jaggery / gud'], 'instructions': 'firstly in a pan dry roast 1 cup sesame on low flame till it splutters.
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ^
SyntaxError: unterminated string literal (detected at line 2)

2

Answers


  1. Try this

      import ast
      import json
    
      # Correct the string to properly use triple quotes
      json_string = """{'author': 'Hebbars Kitchen'}"""
    
      try:
      # Step 1: Parse the string into a Python dictionary
      data = ast.literal_eval(json_string)
    
      # Step 2: Serialize the dictionary into a valid JSON string
      valid_json = json.dumps(data)
    
      # Step 3: Print or update the database with `valid_json`
      print(valid_json)
    
      # Save to database.....
     
      except (ValueError, SyntaxError) as e:
         print("Error parsing the string:", e)
    

    Use of ast.literal_eval()

    Security: ast.literal_eval() only evaluates literals and expressions that are safe and commonly found in data formats like JSON (e.g., strings, numbers, lists, dicts).

    Safety: Using eval() on untrusted input can be dangerous because it can execute arbitrary code. ast.literal_eval() avoids this risk by limiting the types of expressions it can evaluate.

    Login or Signup to reply.
  2. The embedded newline characters will cause literal_eval to fail.

    You could simply remove them and get your desired result as follows:

    import ast
    import json
    d = ast.literal_eval(json_string.replace("n", "")
    print(json.dumps(d, indent=2))
    

    Thus d will refer to a valid Python dictionary

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search