cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

How to Transform SQL Server Records to JSON usable format

bradwyatt
New Contributor II

I am querying transactions_table and batch_table using SQL Server Execute snap. The batch_table returns 1 record, and in this example the transactions_table returns 2 records. I need to be able to pull the data into a JSON-format that is compatible with my companyโ€™s API.

Below is an example format. The โ€œinterfaceโ€ keyโ€™s values are manually entered by me. The โ€œbatch_tableโ€ key contains the values from the one record in the batch_table from SQL Server. The โ€œtransactions_tableโ€ key contains a list of all the records from the transactions_table.

What would be the most efficient way of implementing this?

{
    "interface" : {
        "type" : "INVOICE",
        "client": SQLServer
    },
    "data": {
        "batch_table": {
          "BATCH_ID": 1
        },
        "transactions_table": [{
          "TRANSACTION_ID": 1,
          "AMOUNT": 5.90
          }, 
          {
          "TRANSACTION_ID": 2,
          "AMOUNT: 11.20
        }]
    }
}
6 REPLIES 6

skatpally
Former Employee

It can be achieve this through Velocity Template.

In the JSON Generator snap you can put the template of your choice and refer the field names from the upstream.

Sample

[
{
โ€œrootdocโ€:
{
โ€œfieldname1โ€:$value
}
}

]

bradwyatt
New Contributor II

Thanks for the response.

If I do your suggestion, the output would be:
[
{
โ€œrootdocโ€:
{
โ€œfieldname1โ€:VALUE1
}
},
{
โ€œrootdocโ€:
{
โ€œfieldname1โ€:VALUE2
}
}

]

But, I would rather want the following output:
[
{
โ€œrootdocโ€:
[{
โ€œfieldname1โ€:VALUE1
},
{
โ€œfieldname1โ€:VALUE2
}]
}

]

skatpally
Former Employee

Thatโ€™s just a sample. We can do achieve the desired o/p with looping in Velocity Template.

bradwyatt
New Contributor II

Appreciate the help so far. Can you give me an example snippet (with looping as you mentioned) using Velocity that is similar to what Iโ€™m asking for? Or if you have no examples, how would you use Velocity to code the below?

[
{
โ€œrootdocโ€:
[{
โ€œfieldname1โ€:VALUE1
},
{
โ€œfieldname1โ€:VALUE2
}]
}

]