Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erlnn-0005U0-MB for pgsql-general@arkaria.postgresql.org; Fri, 02 Mar 2018 14:36:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1erlnm-000806-Gk for pgsql-general@arkaria.postgresql.org; Fri, 02 Mar 2018 14:36:50 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erlin-0007DO-W7 for pgsql-general@lists.postgresql.org; Fri, 02 Mar 2018 14:31:42 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1erlik-0002ow-3j for pgsql-general@postgresql.org; Fri, 02 Mar 2018 14:31:40 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 648A920D4A; Fri, 2 Mar 2018 09:31:36 -0500 (EST) Received: from frontend1 ([10.202.2.160]) by compute6.internal (MEProxy); Fri, 02 Mar 2018 09:31:36 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=tCOpS9AqP5UKxvnwzxg14Gu+QG053 0/RbB2N/ba+Sps=; b=jBzVXfGoo3WPBhRxQ2l+SUfgaTrd5G+0qeMeztGI/IQGq 2Oem3tmRAc21a/H+f6Vdk4oRfF1hGfLXh7lduzdz6rXX6lc95FVV5lOydPE4nb7y hiYANNUydTsXe6DWjvbx0l6Na3ovDugVyLnPC8Lm2izJuY45BBQiJ9UQVZaf9Rlj 7pItIgLzCIE65imQ2AqweDs3yaQ/9qTdJHlAHQbFRMVu9GisGxzG2XlGCY6O02JV PC9bls3xkxT4WZpdhG8mmusf5IzX7UlQTum3ImuosAItQJ6FktHttOySJrMN3d/Z smpezgKk09nYGXbI3iWv04tAiVZfAvk0W/IITHPuA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; bh=tCOpS9 AqP5UKxvnwzxg14Gu+QG0530/RbB2N/ba+Sps=; b=MYRUigd1sQ59h04lFRrPmA Uyt1sWDxOnNitQudT3nLEXlHqm/8zV4EqdMUIGZIYvekTZxolU4l4qcrefYknxV9 TOhMeIddtxWJESZUY5YacubGqeVaFSz0gAux79F3B4kRWELeVmZoOf2G2qtnElxX qqPJFdZOFKqS1NWObPsptF4s4rsjS24clZqh4yr42BWPlfbsMNbE3uK/xltu2+aH OLF5rpl9Rw5wpeGhy+M+AtfiRX3JMucXKAP1vdFEwFMzXHkZOU7SbVsCecjR7q5G yyOPuNTCZgASKSbPA8vg6HUktJ5O97BfLzog3lfBkUsr5AgdyzQYspJvjczTL5zw == X-ME-Sender: Received: from [192.168.1.2] (71-35-123-124.tukw.qwest.net [71.35.123.124]) by mail.messagingengine.com (Postfix) with ESMTPA id CB0937E6BC; Fri, 2 Mar 2018 09:31:35 -0500 (EST) Subject: Re: jsonb_array_length: ERROR: 22023: cannot get array length of a scalar To: Alexander Farber Cc: pgsql-general References: <0325b36d-f990-deb9-3086-0fb626c95e14@aklaver.com> From: Adrian Klaver Message-ID: <39bf9413-1465-9880-24e0-ee8d1c84e8a0@aklaver.com> Date: Fri, 2 Mar 2018 06:31:34 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.6.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 03/02/2018 06:14 AM, Alexander Farber wrote: > Hi Adrian, thank you for the reply - > > #  select mid, jsonb_array_length(tiles) from words_moves where > gid=609; > ERROR:  22023: cannot get array length of a scalar > LOCATION:  jsonb_array_length, jsonfuncs.c:1579 > > What am I doing wrong here please? > > > Are you sure all the values in tiles are correctly formatted because > when I use jsonb_array_length with the provided data: > > test=# select jsonb_array_length( '[{"col": 3, "row": 7, "value": 2, > "letter": "С"}, {"col": 4, "row": 7, "value": 1, "letter": "О"}, > {"col": 5, "row": 7, "value": 2, "letter": "П"}, {"col": 6, "row": > 7, "value": 0, "letter": "Л"}, {"col": 7, "row": 7, "value": 3, > "letter": "Я"}]'); >  jsonb_array_length > -------------------- >                   5 > > > I fill that table with the following stored function (please pardon the > huge listing): The little gray cells are not awake enough to work through the below:) If it where me I would first confirm there was malformed data by looking at the data itself. If there are not that many records for gid = 609 maybe a simple select of tiles would be sufficient. Otherwise maybe a simple plpgsql function that loops through the records applying jsonb_array_length and raising a notice on the error. In any case the point is to identify the presence of malformed data and if present the nature of the malformation. That would help reverse engineer any issues with below. > > CREATE OR REPLACE FUNCTION words_play_game( >                 in_uid   integer, >                 in_gid   integer, >                 in_tiles jsonb >         ) RETURNS table ( >                 out_uid  integer, -- the player to be notified >                 out_fcm  text, >                 out_apns text, >                 out_adm  text, >                 out_body text >         ) AS -- Adrian Klaver adrian.klaver@aklaver.com