Accidentally stored string instead of object in Postgres JSONB column

时间:2016-02-12 21:22:25

标签: postgresql jsonb

I just realized that the data being inserted into a JSONB column is a string like buildMenu: function(items) { var node = new Array(); items.forEach(function(elem,index,arr) { node.push(elem.Name); if (elem.SubBranch.length > 0) { return this.buildMenu(elem.SubBranch); //error is here } }, this); return node; } instead of an object like "{\"foo\": 3}". I'm pretty sure the cause is due to setting the SQLAlchemy model's field with

{"foo": 3}

instead of

reading.blob = blob_json

I haven't been able to figure out a way to correct the existing data, though. The Postgres docs give examples of converting text to JSON, extracting array and object elements out of JSON, etc., but I can't find anything that shows how to select the text of a JSONB column that has only one string value.

2 个答案:

答案 0 :(得分:1)

是的,就在我得到这个时:

SELECT (('"{\"foo\":42}"'::JSONB)->>0)::JSONB;

答案 1 :(得分:0)

尤里卡! #postgres上的DeciBull想出了

SELECT (json_build_array('"{\"Stuff.Hello\": 0, \"Stuff.Hello2\": 1000}"'::jsonb)->>0)::jsonb

{"Stuff.Hello": 0, "Stuff.Hello2": 1000}