1

In my mysql table, one of the fields holds data of the nature:

{"gateway":"somevalue","location":"http://www.somesite.org/en/someresource","ip":"100.0.0.9"}

I need to extract the value of the location attribute alone from this field, which is

http://www.somesite.org/en/someresource

in the this case. How do I write a query to achieve this?

1
  • 1
    That's a very good example (and motivation) of how you should NOT save information into the DB! Commented Dec 9, 2013 at 5:55

1 Answer 1

3

Apart from the fact that you better off not storing delimited values of any form (including JSON) in the database, but rather normalize your data, you can leverage very handy SUBSTRING_INDEX() function in the following way

SELECT TRIM(BOTH '"' FROM SUBSTRING_INDEX(SUBSTRING_INDEX(column_name, '"location":', -1), ",", 1)) location
  FROM table_name
 WHERE ...
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.