Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

Convert JSON array to SQL database

avatar
New Contributor

Hi all,

 

I'm very new to NiFi and trying to find ways how to convert json array to be stored in SQL database. I currently have this input json

 

 

{
  "QueryHandle" : "#qb9ec959946924620a6f46013d745fe03",
  "Rows" : [ {
    "Values" : [ "A1", "Test Data A1" ]
  }, {
    "Values" : [ "A2", "Test Data A2" ]
  }, {
    "Values" : [ "A3", "Test Data A3" ]
  }, {
    "Values" : [ "A4", "Test Data A4" ]
  } ],
  "TotalRowCount" : 197,
  "DataTypes" : {
    "Values" : [ {
      "ColumnName" : "Value",
      "Type" : "String"
    }, {
      "ColumnName" : "Description",
      "Type" : "String"
    } ]
  }
}

 

 

Here's the end result I would like to have before I can use PutSQL processor

 

[ {
  "VALUES" : "A1",
  "DESCRIPTION" : "Test Data A1"
}, {
  "VALUES" : "A2",
  "DESCRIPTION" : "Test Data A2"
}, {
  "VALUES" : "A3",
  "DESCRIPTION" : "Test Data A3",
}, {
  "VALUES" : "A4",
  "DESCRIPTION" : "Test Data A4",
}

 

 

I have tried to use jolttransform, but couldn't be able to remove the array or to add the field name to the data. Eventually, I would like to see this in my DB

 

VALUEDESCRIPTION
A1Test Data A1
A2Test Data A2
A3Test Data A3
A4Test Data A4

 

Thank you in advance.

1 ACCEPTED SOLUTION

avatar

Hi,

I think this can be achieved using the following flow:

(Data Source) -> SplitJson ->JoltTransformationJSON ->ConcertJSONToSQL->PutSQL

 

The Configuration for each processor is as follows:

 

1- SplitJson (To get Each of the Rows Element as flowfile)

JsonPath Expression = $.Rows

SAMSAL_0-1675442122040.png

2-JoltTransformationJSON (Convert Each Row Values Element to proper format):

Example Input: { "Values": [ "A4", "Test Data A4"] }

Output: { "VALUE" : "A4", "DESCRIPTION" : "Test Data A4" }
Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "Values": {
        "0": "VALUE",
        "1": "DESCRIPTION"
      }
    }
  }
]

Note: The Output Json Keys has to match the column name in the SQL table

 

3- ConverJSONToSQL ( This is to convert the jolt output json to sql statement that will feed into the PutSql

SAMSAL_1-1675442622650.png

Note: you need to Populate the JDBC Connection Pool

 

4- PutSql (used to execute the insert sql statement generated from above into the DB Table.

SAMSAL_2-1675442714031.png

Note: You need to create the JDBC Connection Pool. SQL Statement Property remains empty to use the SQL statement from the flowfile generated in step 3.

 

If that helps please accept solution.

Thanks

View solution in original post

2 REPLIES 2

avatar

Hi,

I think this can be achieved using the following flow:

(Data Source) -> SplitJson ->JoltTransformationJSON ->ConcertJSONToSQL->PutSQL

 

The Configuration for each processor is as follows:

 

1- SplitJson (To get Each of the Rows Element as flowfile)

JsonPath Expression = $.Rows

SAMSAL_0-1675442122040.png

2-JoltTransformationJSON (Convert Each Row Values Element to proper format):

Example Input: { "Values": [ "A4", "Test Data A4"] }

Output: { "VALUE" : "A4", "DESCRIPTION" : "Test Data A4" }
Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "Values": {
        "0": "VALUE",
        "1": "DESCRIPTION"
      }
    }
  }
]

Note: The Output Json Keys has to match the column name in the SQL table

 

3- ConverJSONToSQL ( This is to convert the jolt output json to sql statement that will feed into the PutSql

SAMSAL_1-1675442622650.png

Note: you need to Populate the JDBC Connection Pool

 

4- PutSql (used to execute the insert sql statement generated from above into the DB Table.

SAMSAL_2-1675442714031.png

Note: You need to create the JDBC Connection Pool. SQL Statement Property remains empty to use the SQL statement from the flowfile generated in step 3.

 

If that helps please accept solution.

Thanks

avatar
New Contributor

Thank you very much!!