0

I have postgreSQL 9.3 and working with json, my field json in DB looks like:

{
    "route_json": [
        {
            "someKeys": "someValues",
            "time": 123
        },
        {
            "someKeys": "someValues",
            "time": 123
        }, ... N
    ]
}

In my case I need to catch the 'time' element from each element of route_json array and set them in new array. Is there any way to do this.

1 Answer 1

3

It’s not pretty:

SELECT
  value->'time'
FROM 
  json_array_elements('{"route_json": [{"someKeys": "someValues","time": 123},{"someKeys": "someValues","time": 456}]}'::json->'route_json');
Sign up to request clarification or add additional context in comments.

1 Comment

On the contrary, that's gorgeous. Only ugly part is the JSON itself. Here's what it looks like if the JSON is a variable: SELECT value->'time' FROM json_array_elements(my_json->'route_json');.

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.