How to convert PostgreSQL 9.4's jsonb type to float

前端 未结 7 636
误落风尘
误落风尘 2020-12-14 14:36

I\'m trying the following query:

SELECT (json_data->\'position\'->\'lat\') + 1.0 AS lat FROM updates LIMIT 5;

(The +1.0 is just there

相关标签:
7条回答
  • 2020-12-14 15:11

    Adding a clarification because this comes up as the top hit for a 'JSONB float conversion' search - note that you need to wrap the JSON conversion in brackets, and then apply the '::' casting.

    As mentioned above, the correct method is:

    (json_data #>> '{field}')::float
    

    If instead you try this it will fail:

    json_data #>> '{field}'::float
    

    This was the mistake I was making in my code and it took me a while to see it - easy fix once I noticed.

    0 讨论(0)
提交回复
热议问题