Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iL3ub-0008QO-6X for pgsql-general@arkaria.postgresql.org; Thu, 17 Oct 2019 11:25:45 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iL3uZ-0003mg-5J for pgsql-general@arkaria.postgresql.org; Thu, 17 Oct 2019 11:25:43 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iL3uY-0003m7-OZ for pgsql-general@lists.postgresql.org; Thu, 17 Oct 2019 11:25:42 +0000 Received: from mail-wr1-x443.google.com ([2a00:1450:4864:20::443]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iL3uV-000771-Do for pgsql-general@lists.postgresql.org; Thu, 17 Oct 2019 11:25:41 +0000 Received: by mail-wr1-x443.google.com with SMTP id r3so1917241wrj.6 for ; Thu, 17 Oct 2019 04:25:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cybertec-at.20150623.gappssmtp.com; s=20150623; h=message-id:subject:from:to:date:in-reply-to:references:user-agent :mime-version:content-transfer-encoding; bh=gVZBRoohGggCosU688x067h7ppSTkeziN6NT8OKov6k=; b=IotxPWpQuwuFDYOWPkROFcI/xXZpJxyfl0JksMjxg9yVjCfkk3sFJw3EWxTqm1MZ5Y NZAL+k+9LWZAbbbg88u2QEgaN1Xrz4XPEwzVU78VzzJPl3pzihVomcz0HkSYZtdPqB4e //QsaAjMy2A6XXpQa0rz0XajzYkihiUdUswhhUiXuquJQYYUpia0rj9MDGODhSf/E3g6 RdkVwZR8yyknVMkm2SdnWYLjum+OXL7SOH0pHUHabjBuHNix0jujqS7OwshBvRyf4ORu JrZwB9KU9d8Dtdi0CUun7UNvZm7ZOjq/Px8nXCNxeawVNMqU57yTVDmc9ps6IIuQRU6w 8CjQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:message-id:subject:from:to:date:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=gVZBRoohGggCosU688x067h7ppSTkeziN6NT8OKov6k=; b=PKfjcq2Xxi1FaOLhXSHNzQ/QTpOm+OgqORo2vDONJEjZh4XldDjrtScEXYCGzMX0Mq Sfdekw/zTx5Rf5yxWnP2f+RYGrt3fsMtyrBV/H0Nu8I8VT0uHUhgsymDOgK8+GRvhiBy cw+tWGJXjfFYDvDVzG0g/vk1CHs/j/nuwmhiHYSWqfDOcPI0iYC1uCA3Qdvcz3l71GEC Vd8b3Hdy6ZuxqwqMvMbtVc5SZcYl6W6NotVZmIjQxNzkQSc2vwS3z+Ka8uRRYFAN5UXb wX1duwkxcelFHKo/F8vukybUjTxN8h3CO4zJrDtyGZBjVPTr2Ik0nqr0Ld/JPEaxd9kc 6Dig== X-Gm-Message-State: APjAAAVzc2VaObmXMAA3t3R7+HazXS4bkwP4lNPl7AkWq0cUiSD8VZpP AbmZ5++H+XbZeVNqLg2Nc8Aknw== X-Google-Smtp-Source: APXvYqzaGOAMp7SWi3N/jNwa1Dcb2ouFn1yf3QE6S6e7v+a6/hHJPcYPN9canSMiQXmDnoczBUo3eQ== X-Received: by 2002:adf:a54e:: with SMTP id j14mr2782431wrb.265.1571311537578; Thu, 17 Oct 2019 04:25:37 -0700 (PDT) Received: from localhost.localdomain ([213.215.245.62]) by smtp.gmail.com with ESMTPSA id u1sm1819158wru.90.2019.10.17.04.25.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 17 Oct 2019 04:25:36 -0700 (PDT) Message-ID: <1df6657c0e93e58791273e6389a2500b15efd0cd.camel@cybertec.at> Subject: Re: A little confusion about JSON Path From: Laurenz Albe To: Thomas Kellerer , pgsql-general@lists.postgresql.org Date: Thu, 17 Oct 2019 13:25:36 +0200 In-Reply-To: <8dcb9b9d-4863-b7e9-c3dc-e4ab2d01e918@gmx.net> References: <8dcb9b9d-4863-b7e9-c3dc-e4ab2d01e918@gmx.net> Content-Type: text/plain; charset="UTF-8" User-Agent: Evolution 3.32.4 (3.32.4-1.fc30) MIME-Version: 1.0 Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Thomas Kellerer wrote: > I don't understand why the following two JSON Path expressions aren't doing the same thing in Postgres 12: > > with sample (data) as ( > values > ('{"k1": {"list":[1,2,3]}}'::jsonb) > ) > select data, > jsonb_path_exists(data, '$.k1.list.type() ? (@ == "array")'), -- returns true as expected > jsonb_path_exists(data, '$.k1.list ? (@.type() == "array")') -- returns false - not expected > from sample; > > > Apparently "@.type()" returns something different then "$.k1.list.type()" > > But maybe I simply don't understand how the @ is supposed to work. This seems to be a consequence of "lax" mode: "Besides, comparison operators automatically unwrap their operands in the lax mode, so you can compare SQL/JSON arrays out-of-the-box. An array of size 1 is considered equal to its sole element. Automatic unwrapping is not performed only when: - The path expression contains type() or size() methods that return the type and the number of elements in the array, respectively. (from https://www.postgresql.org/docs/12/functions-json.html) with sample (data) as ( values ('{"k1": {"list":[1,2,3]}}'::jsonb) ) select data, jsonb_path_exists(data, '$.k1.list ? (@.type() == "number")'), -- lax mode unwraps the array jsonb_path_exists(data, 'strict $.k1.list ? (@.type() == "array")') -- strict mode doesn't from sample; data | jsonb_path_exists | jsonb_path_exists -----------------------------+-------------------+------------------- {"k1": {"list": [1, 2, 3]}} | t | t (1 row) Yours, Laurenz Albe -- Cybertec | https://www.cybertec-postgresql.com