Extracting Key/Value Pairs JSON Structure into Tabular Format

02 September 2020
KingswaySoft Team

When working with JSON data, a scenario may arise where there could be a format of a JSON Document that makes it challenging to extract the data into a typical tabular form. One of these formats is Key-Value Pairs that some APIs use. In this blog post, we will discuss using the "Key Value Pivot" Output type found in our JSON Source/Extract Components to parse Key/Value pairs from JSON documents with ease.

JSON Key Value Pair Format

First, to demonstrate the difference between a typical JSON format and the Key/Value pair structure, consider the following typical JSON Document design:

[
	{
		"Id":173,
		"FirstName":"Rudolph",
		"LastName":"Glover",
		"Gender":"Male",
		"Email":"[email protected]"},
		"City":"Kampong",
		"Country":"Vietnam"
	}
]

When working with a similar document design, as shown above, we can directly associate the column name with the JSON Node name, and the Value of each node will be the data value under its corresponding column. This format can be easily parsed into a tabular form in SSIS. However, the Key/Value structure could contain the same data but in a different way. To understand the difference, consider the following JSON Document instead:

[
   {
      "Id":173,
      "properties":[
         {
            "Name":"FirstName",
            "Value":"Rudolph"
         },
         {
            "Name":"LastName",
            "Value":"Glover"
         },
         {
            "Name":"Gender",
            "Value":"Male"
         },
         {
            "Name":"Email",
            "Value":"[email protected]"
         },
         {
            "Name":"City",
            "Value":"Kampong"
         },
         {
            "Name":"Country",
            "Value":"Vietnam"
         }
      ]
   }
]

As we can see from the example above, the Column names are actually values for the "Name" node in the properties array. In contrast, the "Value" node will contain the data values of the corresponding Column based on the value assigned to the Name node. This structure makes it challenging to extract this JSON document into a typical tabular format. For instance, if we try to extract data from this document in SSIS, it will be retrieved as follows:

Extracing Key/Value JSON Data in SSIS

As we can see from the table shown above, this may not be the standard tabular format we expect to integrate the data from our JSON document to another database system or application.

Therefore, since version 20.1 of our SSIS Productivity Pack, we have added a new "Key Value Pivot" Output Type in our JSON Source/Extract components, which can be used to work with these types of JSON document designs.

Using the "Key Value Pivot" feature in a JSON Source/Extract Component

First, when working with our JSON Source Component, we need to configure the Data Source page of the component. For the Connection Manager property, we can choose the <<Local File>> option when working with a JSON file located on our file system. On the other hand, If we are receiving a JSON Document from an API, then we would choose an existing HTTP Connection Manager instead.

In this example, we are reading our JSON Document from a local file, and we have selected the file location in the Local File Path property, as shown below:

JSON Source Editor from Local File

Once this has been done, we can set the design of the JSON document in the Document Designer Page of the JSON Source/Extract Component. We do this by defining each node manually, or we can use the Import feature of our JSON/Extract component to infer the design of the JSON Document based on a sample JSON file. Then, we can perform the necessary changes in the Document Designer settings to read the unique Key/Value Pair format.

Import JSON Local File

After the import is complete, the component will set the Design of the document as follows, using the JSON document we have mentioned earlier in the blog post as an example.

JSON Design

We then change the Output type of the "properties" array to "Key Value Pivot" as shown below. We are going to set the "Name" node's Output Type to Key, and the "Value" node's Output Type to Value.

JSON Change Output Type to Key Value Pivot

Once this has been done, we click on the Output Settings cell next to the "properties" array to open the Key Value Pivot Editor. We then define all the expected Keys which might be returned under the properties array.

Key Value Pivot Editor

Therefore, the final Document Design should be as shown in the screenshot below:

JSON Source Editor with Key Value Pivot Complete

The component will create all the necessary columns based on the Keys we have defined in the Key Value Pivot Editor. We can verify this by opening the Columns page of the component.

JSON Source Editor Columns Confirmation

Finally, when we execute our SSIS package. We can see that the JSON Source/Extract Component has correctly parsed the Key/Value pair format into a typical tabular format as shown below:

Root Data View at JSON Source

Closing Notes

By working with the new "Key Value Pivot" Output Type in our JSON Source/Extract components, we were able to extract the JSON data, which was in a Key/Value pair format into a typical tabular form. Therefore, it will be easier to integrate our JSON data with any other database system or application. This demonstrates the flexibility of our JSON components when working with different structures of JSON documents that some APIs may use.

Archive

November 2024 3 October 2024 1 September 2024 1 August 2024 2 July 2024 1 June 2024 1 May 2024 1 April 2024 2 March 2024 2 February 2024 2 January 2024 2 December 2023 1 November 2023 1 October 2023 2 August 2023 1 July 2023 2 June 2023 1 May 2023 2 April 2023 1 March 2023 1 February 2023 1 January 2023 2 December 2022 1 November 2022 2 October 2022 2 September 2022 2 August 2022 2 July 2022 3 June 2022 2 May 2022 2 April 2022 3 March 2022 2 February 2022 1 January 2022 2 December 2021 1 October 2021 1 September 2021 2 August 2021 2 July 2021 2 June 2021 1 May 2021 1 April 2021 2 March 2021 2 February 2021 2 January 2021 2 December 2020 2 November 2020 4 October 2020 1 September 2020 3 August 2020 2 July 2020 1 June 2020 2 May 2020 1 April 2020 1 March 2020 1 February 2020 1 January 2020 1 December 2019 1 November 2019 1 October 2019 1 May 2019 1 February 2019 1 December 2018 2 November 2018 1 October 2018 4 September 2018 1 August 2018 1 July 2018 1 June 2018 3 April 2018 3 March 2018 3 February 2018 3 January 2018 2 December 2017 1 April 2017 1 March 2017 7 December 2016 1 November 2016 2 October 2016 1 September 2016 4 August 2016 1 June 2016 1 May 2016 3 April 2016 1 August 2015 1 April 2015 10 August 2014 1 July 2014 1 June 2014 2 May 2014 2 February 2014 1 January 2014 2 October 2013 1 September 2013 2 August 2013 2 June 2013 5 May 2013 2 March 2013 1 February 2013 1 January 2013 1 December 2012 2 November 2012 2 September 2012 2 July 2012 1 May 2012 3 April 2012 2 March 2012 2 January 2012 1

Tags