、
- あなたがキーと値を持っている場合は、文字列からキャストするリテラルJSONオブジェクトを書いているならば、それは適切でなければならないオブジェクト
- を構築するために
jsonb_build_object
を使用します。適切なJSON文字列にはエスケープする必要があります\
。
説明
PostgreSQLはこれを引用されていません:それはRFC 7159次だけのJSON implimentationです。
A string begins and ends with quotation marks. All Unicode characters may be placed within the quotation marks, except for the characters that must be escaped: quotation mark, reverse solidus, and the control characters (U+0000 through U+001F). [... ] So, for example, a string containing only a single reverse solidus character may be represented more compactly as "\\"
.
これはリテラル形式のようです。 \+
はJSONで有効な文字列ではありませんので、だから、この意志ない仕事を
CREATE TABLE tbl
AS
SELECT '{"entry":"((\\+)[0-9]+)"}'::jsonb AS info;
ドル引用符のPostgreSQLには無エスケープを必要としませんが、それはここでは助けにはなりません、
Notice that inside the dollar-quoted string, single quotes can be used without needing to be escaped. Indeed, no characters inside a dollar-quoted string are ever escaped: the string content is always written literally. Backslashes are not special, and neither are dollar signs, unless they are part of a sequence matching the opening tag.
。しかし、私たちがjsonタイプを使用していない場合にはうまくいくでしょう。
SELECT '{"key":"\"}'::jsonb;
ERROR: invalid input syntax for type json
LINE 1: SELECT '{"key":"\"}'::jsonb;
^
DETAIL: Token ""\"}" is invalid.
CONTEXT: JSON data, line 1: {"key":"\"}
ただし、
SELECT FORMAT($${%s:%s}$$, to_jsonb(k), to_jsonb(v))::jsonb
FROM (VALUES
('key', '\')
) AS t(k,v);
.. JSON-エスケープ文字列にto_jsonb()
を使用することができます。しかし、あなたはキーと値を持っている場合はjson_build_object
、
を使用できるためにも、これは悪い考えです
SELECT jsonb_build_object(k, v)
FROM (VALUES
('key', '\')
) AS t(k,v);
これが所属する場所を取得し、[dba.se]に移行するフラグを設定してください。 –