0

I am trying to find a way to parse a JSON string and save them into mysql. This is my json!

{"title": My title, "desc": mydesc, "url": http//example.com}

From now i don't have problem to save all json into one column usining json.dumps() so actually I'm trying to parse each joson data string to send him to mysql table. Title | Desc | Url.

This is my python code for desc example (pyspider-resultdb.py)

def _parse(self, data):
    for key, value in list(six.iteritems(data)):
        if isinstance(value, (bytearray, six.binary_type)):
            data[key] = utils.text(value)
    if 'result' in data:
        decoded = json.loads(data['result'])
        data['result'] = json.dumps(decoded['desc'])
    return data

def _stringify(self, data):
    if 'result' in data:
         decoded = json.loads(data['result'])
         data['result'] = json.dumps(decoded['desc'])
    return data
4
  • And what is your question? Commented Feb 3, 2015 at 17:57
  • 3
    This is not valid JSON. String values should have quotes around them: {"title": "My title"} Commented Feb 3, 2015 at 18:01
  • It depends on the DB wrapper/connector You are using. Don't dump the dict, take advantage of the key-value structure and use it to update multiple columns in a single query. Commented Feb 3, 2015 at 18:01
  • "myaut" You have the right, I forgot to put the quotes :( Commented Feb 3, 2015 at 18:11

1 Answer 1

1

It's unclear from your question what you trying to achieve, but If your question is how to convert JSON to python dict and then load to the table, then that's how you can do it:

my_dict = json.loads('{"title": "foo", "dest": "bar"}')
curs.execute('INSERT INTO test (title, dest) values(%(title)s, %(dest)s)', my_dict)
Sign up to request clarification or add additional context in comments.

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.