I have been trying to figure out why mysql shows me the next error.
CALL syncSAP_ncmCodes("[{\"AbsEntry\": 1,\"NcmCode\": \"01010101\",\"Descrip\": \"No existe en el catálogo\", \"GroupCode\": \"S\"}]") Error Code: 3141. Invalid JSON text in argument 1 to function json_extract: "The document root must not follow by other values." at position
When I call it in this way
CALL syncSAP_ncmCodes("[{\"AbsEntry\": 1,\"NcmCode\": \"01010101\",\"Descrip\": \"No existe en el catálogo\", \"GroupCode\":\"S\"}]");
this is the SP
CREATE DEFINER=`admin`@`%` PROCEDURE `syncSAP_ncmCodes`(IN json_response mediumtext)
BEGIN
set @tot_elements = JSON_LENGTH(json_response);
set @cts = CURRENT_TIMESTAMP();
set @i = 0;
while @i < @tot_elements do
SET @ncmCode = json_response->"$[0]";
set @AbsEntry= ITEM_JSON(@ncmCode, '$.AbsEntry');
set @NcmCode= ITEM_JSON(@ncmCode, '$.NcmCode');
set @Descrip= ITEM_JSON(@ncmCode, '$.Descrip');
set @GroupCode= ITEM_JSON(@ncmCode, '$.GroupCode');
INSERT INTO `IK-Inventario`.`cat_sap_ncmCodes`
(
`AbsEntry`,`NcmCode`,
`Descrip`,`GroupCode`,
`ncmCodeSource`,
`enabled`,
`created_by`,`created_at`,`updated_at`,`updated_by`
)
VALUES
(
@AbsEntry,
@NcmCode,
@Descrip,
@GroupCode,
"SAP",
1,
"SYSTEM",@cts,@cts,"SYSTEM"
)
ON duplicate key update
`AbsEntry`= @AbsEntry,
`NcmCode`= @NcmCode,
`Descrip`= @Descrip,
`GroupCode`= @GroupCode,
`ncmCodeSource`="SAP",
`enabled`=1,
`updated_at`= @cts,
`updated_by`="SYSTEM";
set @i = @i +1;
end while;
select concat("RDS:syncncm_Codes:",@cts,": ",@i," records processed.") AS result;
END
this is the table
CREATE TABLE `cat_sap_ncmCodes` (
`AbsEntry` int(11) NOT NULL,
`NcmCode` varchar(45) NOT NULL,
`Descrip` varchar(100) DEFAULT NULL,
`GroupCode` varchar(45) DEFAULT NULL,
`ncmCodeSource` varchar(45) DEFAULT NULL,
`created_by` varchar(45) DEFAULT NULL,
`created_at` datetime DEFAULT NULL,
`updated_by` varchar(45) DEFAULT NULL,
`updated_at` datetime DEFAULT NULL,
`enabled` tinyint(1) DEFAULT NULL,
PRIMARY KEY (`NcmCode`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
This is the ITEM_JSON function
CREATE DEFINER=`admin`@`%` FUNCTION `ITEM_JSON`(jsonData TEXT, prop VARCHAR(100)) RETURNS text CHARSET utf8
BEGIN
DECLARE theValue TEXT;
#SELECT JSON_UNQUOTE(JSON_EXTRACT(jsonData, prop)) INTO theValue;
#RETURN theValue;
RETURN JSON_UNQUOTE(JSON_EXTRACT(jsonData, prop));
END
I've already try to use the JSON_EXTRACT function with the same argument from a isolate sql script and its working, but when I try to use it within the SP shows me that error.
Can somebody help me, giving me some idead where I can search?