1

I have a column of JSON strings in my SQL table. I want to extract the 'page' value, any idea how? {"action.type":"click","page":1424}

1
  • 1
    Just got a pure SQL version working. Commented Jun 13, 2012 at 19:26

2 Answers 2

2

Hive actually has a command just for that get_json_object

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

Comments

2

Here is a pure SQL version.

DECLARE @json varchar(64);
DECLARE @index int;
SELECT @json = '{"action.type":"click","page":1424}';
DECLARE @length int = LEN(@json);
DECLARE @pageIndex int = CHARINDEX('page":', @json);
DECLARE @difference int = @length - (@pageIndex + 6); -- 6 is for page":
SELECT @index =  CHARINDEX('page', @json);
SELECT SUBSTRING(@json, @index + 6, @difference);

This will give you a result of 1424

It is really long-winded, but it shows step-by-step how it get's that value. You can easily refactor that into a stored procedure.

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.