community.postgresql icon indicating copy to clipboard operation
community.postgresql copied to clipboard

postgresql_query is not able use variable list as postgreSQL Json

Open sivanagireddyb opened this issue 3 years ago • 9 comments

Summary

I am not able to use the variable list as postgreSQL Json argument in the psql query

PostgreSQL accept Json in format ["[email protected]","[email protected]"] On manipulating variable list ["[email protected]", "[email protected]"]

Error: Invalid input syntax for type json\n LINE 1:... Token \"'\" is invalid

Issue Type

Bug Report

Component Name

postgresql_query

Ansible Version

$ ansible --version

ansible 2.8.2

Configuration

# if using a version older than ansible-core 2.12 you should omit the '-t all'

OS / Environment

CENTOS

Steps to Reproduce

vars:
  contacts: ["[email protected]", "[email protected]" ]

tasks:
- name: Insert to db
  postgresql_query:
    db: xxx
    login_user: xxx
    login_password: xxx
    query: INSERT INTO test_table (id, contacts) VALUES (%s, %s)
    positional_args:
    - 1
    - '{{ contacts }}'

Expected Results

List variable should be inserted

Actual Results

Invalid input syntax for type json\n LINE 1:... Token \"'\" is invalid

sivanagireddyb avatar Aug 04 '22 17:08 sivanagireddyb

thanks for the report @sivanagireddyb . I haven't looked at the code, but does using single-quotes instead of double-quotes fail w/ the same error?

hunleyd avatar Aug 04 '22 17:08 hunleyd

@hunleyd Yes, Double quotes are also giving same error. I had to use json filter and ascii format to make it work. '{{ contacts|to_json(ensure_ascii=False) }}' this worked for me.

sivanagireddyb avatar Aug 05 '22 00:08 sivanagireddyb

ok thanks for the update @sivanagireddyb . we'll look into the issue and see if we can code up a fix.

hunleyd avatar Aug 05 '22 12:08 hunleyd

@sivanagireddyb hello, thanks for reporting the issue. I'm not a Postgres JSON user. I found on https://www.postgresqltutorial.com/postgresql-tutorial/postgresql-json/ that JSON should be inserted as a string of the format '{"key": "value"}' but I see a list in the description.

  • Could you please elaborate a bit on why there's the list in the description?
  • If you pass, say, the string {"email1": "[email protected]", "email2": "[email protected]"}, will it work?

thanks

Andersson007 avatar Aug 10 '22 07:08 Andersson007

@sivanagireddyb could you please take a look? ^ We're looking forward to your feedback

Andersson007 avatar Aug 15 '22 09:08 Andersson007

Even though postgress type is Json it accepts list, Which worked for me when tested with hard-coded value instead of variable.

sivanagireddyb avatar Aug 15 '22 13:08 sivanagireddyb

@sivanagireddyb if you pass the string {"email1": "[email protected]", "email2": "[email protected]"}, will it work?

Andersson007 avatar Aug 16 '22 06:08 Andersson007

I haven't tested it but the the format I was expecting is in list []

sivanagireddyb avatar Aug 16 '22 08:08 sivanagireddyb

Using ascii filter along with json worked for me.

'{{ contacts|to_json(ensure_ascii=False) }}'

sivanagireddyb avatar Aug 16 '22 08:08 sivanagireddyb