How to read properly data from JSON file while inserting data into PostgreSQL using Mule ESB, Mule Studio

StackOverflow https://stackoverflow.com/questions/14008874

  •  11-12-2021
  •  | 
  •  

Question

I have a JSON file as under

[ 
  { "category": "reference",
    "author": "Nigel Rees",
    "title": "Sayings of the Century",
    "price": 8.95
  },
  { "category": "fiction",
    "author": "Evelyn Waugh",
    "title": "Sword of Honour",
    "price": 12.99
  }
]

I am trying to read the file and store the data into PostgreSQL database. My configuration xml file is as under

<jdbc:postgresql-data-source name="PostgreSQL_Data_Source" user="superuser" password="pwd" url="jdbc:postgresql://localhost:5432/TestDB" transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source"/>
    <jdbc:connector name="Database-Connector" dataSource-ref="PostgreSQL_Data_Source" validateConnections="true" queryTimeout="-1" pollingFrequency="0" doc:name="Database">
        <jdbc:query key="InsertQuery" value="INSERT INTO &quot;tblTest&quot;(category,author,title,price)VALUES (?,?,?,?)"/>
    </jdbc:connector>
    <flow name="testxmlFlow1" doc:name="testxmlFlow1">
        <file:inbound-endpoint path="C:\InputFolder" responseTimeout="10000" doc:name="File"/>
        <byte-array-to-string-transformer doc:name="Byte-Array-to-String"/>
        <jdbc:outbound-endpoint exchange-pattern="one-way" queryKey="InsertQuery" queryTimeout="-1" connector-ref="Database-Connector" doc:name="Database"/>
</flow>

I am not trying to store it in a complete JSON column rather want to parse it first and then want to store it in individual table columns i.e. category,author,title,price.

What changes I need to do for that in the configuration file and how can I process that?

Thanks

Était-ce utile?

La solution

Transform the JSON data into a java.util.List then split it into several java.util.Maps and write each of them to the DB.

<jdbc:postgresql-data-source name="PostgreSQL_Data_Source"
    user="superuser" password="pwd" url="jdbc:postgresql://localhost:5432/TestDB"
    transactionIsolation="UNSPECIFIED" doc:name="PostgreSQL Data Source" />

<jdbc:connector name="Database-Connector" dataSource-ref="PostgreSQL_Data_Source"
    validateConnections="true" queryTimeout="-1" pollingFrequency="0"
    doc:name="Database">
    <jdbc:query key="InsertQuery"
        value="INSERT INTO &quot;tblTest&quot;(category,author,title,price)VALUES (#[message.payload.category],#[message.payload.author],#[message.payload.title],#[message.payload.price])" />
</jdbc:connector>

<flow name="testxmlFlow1" doc:name="testxmlFlow1">
    <file:inbound-endpoint path="C:\InputFolder"
        responseTimeout="10000" doc:name="File" />
    <json:json-to-object-transformer
        returnClass="java.util.List" doc:name="JSON to List" />
    <collection-splitter />
    <jdbc:outbound-endpoint exchange-pattern="one-way"
        queryKey="InsertQuery" queryTimeout="-1" connector-ref="Database-Connector"
        doc:name="Database" />
</flow>
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top