2

I have a string:

"""
{
   "description":"123456 asdad asa "0-asd;'asddf1231" "12"  ",
   "sale":12
} 
"""

I want to decode this into JSON, but the description field is ill-formatted. I am thinking about using re.sub to remove all of the inner quotes from the description field, but I've had no luck so far. Does anyone have a good regex hint? Thanks!

7
  • 2
    I assume you have more data, otherwise you could do it manually. We need more data to be able to tell how sophisticated the cleanup code has to be. For this particular string, you could just replace all instances of quoted numbers with just the numbers, but I don't know if that'll work for all your data. Commented Aug 10, 2015 at 0:24
  • yes, production data is wayyyy more complicated. Commented Aug 10, 2015 at 3:05
  • Is "description" always the first key? Starting from "desc, is the relevant text always going to start with "description":"? Is it always going to end with ",? Is it always on it's own line? Commented Aug 10, 2015 at 3:14
  • You can assume "description":" never change and ends with "," Commented Aug 10, 2015 at 3:55
  • I think I will just end up writing a function to parse this manually. Commented Aug 10, 2015 at 3:55

2 Answers 2

2

You can try this, but there should be a better way.

(?:.*?(?={))|(?:(?<=}).*)|(?:(?<!:)"(?!,))

Regex live here.

Sign up to request clarification or add additional context in comments.

Comments

0

I'll just answer this now in case it actually works for you; if it doesn’t, I'll modify this answer.

>>> import json
>>> import re
>>>
>>> s =  """ { "description":"123456 asdad asa "0" "12" ", "sale":12 } """
>>>
>>> new_s = re.sub('"(\d+)"', r'\1', s)
>>> new_s
' { "description":"123456 asdad asa 0 12 ", "sale":12 } '
>>>
>>> d = json.loads(new_s)
>>> d
{u'description': u'123456 asdad asa 0 12 ', u'sale': 12}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.