How to apply regular expression on a JSON field to extract certain values from it and store into PostgreSQL Dataabse fron Mule Studio?

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

Question

Say I have a REST based service that returns a JSON object as under

{
    "AlertDetails":
        {
            "start":0,
            "left":0,
            "_itemList":
                [   
                    {
                        "id":"badntp",
                        "text":"The time cannot be synchronized to the configured time server.",
                        "click":{
                                    "text":"Click here to modify your time settings.",
                                    "url":"datetime.html"
                                },
                        "severity":"warning",
                        "occurred":"20121031T10:18:54",
                        "args":null
                    },
                    {
                        "id":"updatesitefail",
                        "text":"The update site cannot be contacted to determine if software updates are available.",
                        "click":{
                                    "text":"Click here to manually check for updates.",
                                    "url":"http:\\\/\\\/xyz.com\\\/support\\\/xyz.html"
                                },
                        "severity":"info",
                        "occurred":"20121105T17:23:24",
                        "args":"[http:\\\/\\\/xyz.com\\\/support\\\/xyz.html]"
                    }
                ]
        }
}

Now I have a table in PostgreSQL(say testTable) whose structure is as under

ID       (string)
Severity (string)
Occurred (string)

How can I parse those values from the available JSON field to insert the same into the PostgreSQL Database from Mule ESB studio.

The final result in the testTable should be

ID                  Severity        Occured
---------           ---------       --------
"badntp"            "warning"       "20121031T10:18:54"
"updatesitefail"    "info"          "20121105T17:23:24"

My configuration file is as under

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

<jdbc:connector name="PostgreSQL_Connector" dataSource-ref="PostgreSQL_Data_Source"
    validateConnections="true" queryTimeout="-1" pollingFrequency="0"
    doc:name="Database">
    <jdbc:query key="InsertRecord"
        value="INSERT INTO &quot;testTable&quot;(&quot;ID&quot;,&quot;Severity&quot;,&quot;Occured&quot;) VALUES (?,?,?)" />
</jdbc:connector>

<flow name="testRestFlow1" doc:name="testRestFlow1">
    <http:inbound-endpoint exchange-pattern="request-response"
        address="http://localhost:8082/index.html" doc:name="HTTP" />
    <http:rest-service-component httpMethod="GET"
        serviceUrl="http://localhost:35798/RestServiceImpl.svc/json/567" />
    <object-to-string-transformer />
    <jdbc:outbound-endpoint exchange-pattern="one-way"
        queryKey="InsertRecord" queryTimeout="-1" connector-ref="PostgreSQL_Connector"
        doc:name="Database" />
</flow>

The #[message.payload] will contain the entire JSON string. What will come(regular expression or something else) in VALUES (?,?,?) to parse #[message.payload]?

Thanks in advance

Was it helpful?

Solution

From the MEL cheatsheet:

MEL has no direct support for JSON. The json-to-object-transformer can turn a JSON payload into a hierarchy of simple data structures that are easily parsed with MEL.

So use first to transform the JSON into datastructures (maps, lists):

<json:json-to-object-transformer returnClass="java.lang.Object" />

Then we extract the _itemList array:

<expression-transformer
    expression='#[message.payload['AlertDetails']['_itemList']]" />

Then we split this array in individual messages:

<collection-splitter />

From there the following expressions will evaluate to the desired values so they can be used in the insert query:

#[message.payload.id]
#[message.payload.severity]
#[message.payload.occurred]

OTHER TIPS

You can not get your data using Regular expression, because the language of JSON files is not regular (it is context-free). I recommend you to parse it (use some library for that) and then loop through it and read the values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top