Search code examples
sqlapache-nifi

Unable to save JSON to database with field named as order (NiFi)


I have a JSON file:

[ {
  "Order" : "Nestle billboard 100%x250",
  "Country" : "Russia",
  "Order_ID" : 287259619,
  "Country_ID" : 243,
  "Order_lifetime_impressions" : "3385377",
  "Total_unique_visitors" : "1090850",
  "Total_reach_impressions" : "3385525",
  "Average_impressions_unique_visitor" : 3.1,
  "Date" : "2021-07-01"
}, {
  "Order" : "Nestle_june_july 2021_ mob 300x250",
  "Country" : "Russia",
  "Order_ID" : 28734,
  "Country_ID" : 263,
  "Order_lifetime_impressions" : "1997022",
  "Total_unique_visitors" : "1012116",
  "Total_reach_impressions" : "1997036",
  "Average_impressions_unique_visitor" : 1.97,
  "Date" : "2021-07-01"
}]

And table with the same column names. I'm using PutDatabaseRecord processor with this configuration: enter image description here

When I'm trying to save this file, I get an error.

ERROR: syntax error (at or near: ",") Position: 110

I renamed column in the table and in the json to order_name and processor was able to save it.

But I still want to save it as order if it possible.

I really dont understand why this happens. Yes, order is a keyword for sql, but it's inside ". Is it a bug? How can I fix it without renaming columns?

If I will keep Order as column in JSON, but change column name in database - works fine as well. But of course, I cannot save Order to this renamed column.


Solution

  • Order is a reserved word and you should absolutely avoid using it as a column name if you can. [1] [3]

    If you absolutely can't, you need to set the Quote Column Identifiers property to True in the PutDatabaseRecord processor config. [2]

    1. https://www.postgresql.org/docs/current/sql-keywords-appendix.html
    2. https://nifi.apache.org/docs/nifi-docs/components/org.apache.nifi/nifi-standard-nar/1.13.2/org.apache.nifi.processors.standard.PutDatabaseRecord/
    3. Postgres table column name restrictions?