Output Excel data as JSON - Office Scripts (2024)

  • Article

Excel table data can be represented as an array of objects in the form of JSON. Each object represents a row in the table. This helps extract the data from Excel in a consistent format that is visible to the user. The data can then be given to other systems through Power Automate flows.

Setup: Sample Excel file

This workbook contains the data, objects, and formatting expected by the script.

Download the sample workbook

Output Excel data as JSON - Office Scripts (1)

A variation of this sample also includes the hyperlinks in one of the table columns. This allows additional levels of cell data to be surfaced in the JSON.

Output Excel data as JSON - Office Scripts (2)

Sample code: Return table data as JSON

Add the following script to the sample workbook and try the sample yourself!

Note

You can change the interface TableData structure to match your table columns. Note that for column names with spaces, be sure to place your key in quotation marks, such as with "Event ID" in the sample. For more information about working with JSON, read Use JSON to pass data to and from Office Scripts.

function main(workbook: ExcelScript.Workbook): TableData[] { // Get the first table in the "PlainTable" worksheet. // If you know the table name, use `workbook.getTable('TableName')` instead. const table = workbook.getWorksheet('PlainTable').getTables()[0]; // Get all the values from the table as text. const texts = table.getRange().getTexts(); // Create an array of JSON objects that match the row structure. let returnObjects: TableData[] = []; if (table.getRowCount() > 0) { returnObjects = returnObjectFromValues(texts); } // Log the information and return it for a Power Automate flow. console.log(JSON.stringify(returnObjects)); return returnObjects}// This function converts a 2D array of values into a generic JSON object.// In this case, we have defined the TableData object, but any similar interface would work.function returnObjectFromValues(values: string[][]): TableData[] { let objectArray: TableData[] = []; let objectKeys: string[] = []; for (let i = 0; i < values.length; i++) { if (i === 0) { objectKeys = values[i] continue; } let object: {[key: string]: string} = {} for (let j = 0; j < values[i].length; j++) { object[objectKeys[j]] = values[i][j] } objectArray.push(object as unknown as TableData); } return objectArray;}interface TableData { "Event ID": string Date: string Location: string Capacity: string Speakers: string}

Sample output from the "PlainTable" worksheet

[{ "Event ID": "E107", "Date": "2020-12-10", "Location": "Montgomery", "Capacity": "10", "Speakers": "Debra Berger"}, { "Event ID": "E108", "Date": "2020-12-11", "Location": "Montgomery", "Capacity": "10", "Speakers": "Delia Dennis"}, { "Event ID": "E109", "Date": "2020-12-12", "Location": "Montgomery", "Capacity": "10", "Speakers": "Diego Siciliani"}, { "Event ID": "E110", "Date": "2020-12-13", "Location": "Boise", "Capacity": "25", "Speakers": "Gerhart Moller"}, { "Event ID": "E111", "Date": "2020-12-14", "Location": "Salt Lake City", "Capacity": "20", "Speakers": "Grady Archie"}, { "Event ID": "E112", "Date": "2020-12-15", "Location": "Fremont", "Capacity": "25", "Speakers": "Irvin Sayers"}, { "Event ID": "E113", "Date": "2020-12-16", "Location": "Salt Lake City", "Capacity": "20", "Speakers": "Isaiah Langer"}, { "Event ID": "E114", "Date": "2020-12-17", "Location": "Salt Lake City", "Capacity": "20", "Speakers": "Johanna Lorenz"}]

Sample code: Return table data as JSON with hyperlink text

Note

The script always extracts hyperlinks from the 4th column (0 index) of the table. You can change that order or include multiple columns as hyperlink data by modifying the code under the comment // For the 4th column (0 index), extract the hyperlink and use that instead of text.

function main(workbook: ExcelScript.Workbook): TableData[] { // Get the first table in the "WithHyperLink" worksheet. // If you know the table name, use `workbook.getTable('TableName')` instead. const table = workbook.getWorksheet('WithHyperLink').getTables()[0]; // Get all the values from the table as text. const range = table.getRange(); // Create an array of JSON objects that match the row structure. let returnObjects: TableData[] = []; if (table.getRowCount() > 0) { returnObjects = returnObjectFromValues(range); } // Log the information and return it for a Power Automate flow. console.log(JSON.stringify(returnObjects)); return returnObjects}function returnObjectFromValues(range: ExcelScript.Range): TableData[] { let values = range.getTexts(); let objectArray : TableData[] = []; let objectKeys: string[] = []; for (let i = 0; i < values.length; i++) { if (i === 0) { objectKeys = values[i] continue; } let object = {} for (let j = 0; j < values[i].length; j++) { // For the 4th column (0 index), extract the hyperlink and use that instead of text. if (j === 4) { object[objectKeys[j]] = range.getCell(i, j).getHyperlink().address; } else { object[objectKeys[j]] = values[i][j]; } } objectArray.push(object as TableData); } return objectArray;}interface TableData { "Event ID": string Date: string Location: string Capacity: string "Search link": string Speakers: string}

Sample output from the "WithHyperLink" worksheet

[{ "Event ID": "E107", "Date": "2020-12-10", "Location": "Montgomery", "Capacity": "10", "Search link": "https://www.google.com/search?q=Montgomery", "Speakers": "Debra Berger"}, { "Event ID": "E108", "Date": "2020-12-11", "Location": "Montgomery", "Capacity": "10", "Search link": "https://www.google.com/search?q=Montgomery", "Speakers": "Delia Dennis"}, { "Event ID": "E109", "Date": "2020-12-12", "Location": "Montgomery", "Capacity": "10", "Search link": "https://www.google.com/search?q=Montgomery", "Speakers": "Diego Siciliani"}, { "Event ID": "E110", "Date": "2020-12-13", "Location": "Boise", "Capacity": "25", "Search link": "https://www.google.com/search?q=Boise", "Speakers": "Gerhart Moller"}, { "Event ID": "E111", "Date": "2020-12-14", "Location": "Salt Lake City", "Capacity": "20", "Search link": "https://www.google.com/search?q=salt+lake+city", "Speakers": "Grady Archie"}, { "Event ID": "E112", "Date": "2020-12-15", "Location": "Fremont", "Capacity": "25", "Search link": "https://www.google.com/search?q=Fremont", "Speakers": "Irvin Sayers"}, { "Event ID": "E113", "Date": "2020-12-16", "Location": "Salt Lake City", "Capacity": "20", "Search link": "https://www.google.com/search?q=salt+lake+city", "Speakers": "Isaiah Langer"}, { "Event ID": "E114", "Date": "2020-12-17", "Location": "Salt Lake City", "Capacity": "20", "Search link": "https://www.google.com/search?q=salt+lake+city", "Speakers": "Johanna Lorenz"}]

Use in Power Automate

For how to use such a script in Power Automate, see Create an automated workflow with Power Automate.

Output Excel data as JSON - Office Scripts (2024)

FAQs

How to convert Excel data into JSON? ›

Excel to JSON using VBA code editor

Besides using all these tools, you can use the in-built feature of Excel (VBA code editor) to convert the Excel data to JSON format. Make a code for it and execute the code; it will do the mapping of Excel columns to JSON object keys and convert the data into JSON.

How to convert table data into JSON format? ›

Just create a Copy Activity from a database table source to a json file sink without importing the schemas. In the Copy Activity sink settings specify array of objects under File Pattern. It will look like below.

How to return output in JSON format? ›

To return JSON from the server, you must include the JSON data in the body of the HTTP response message and provide a "Content-Type: application/json" response header. The Content-Type response header allows the client to interpret the data in the response body correctly.

How do I export JSON data from Excel? ›

How to Import JSON to Excel: Step-By-Step Guide
  1. Step 1: Prepare your JSON Data. ...
  2. Step 2: Open Excel and Create a New Worksheet. ...
  3. Step 3: Enable the Power Query Add-in. ...
  4. Step 4: Import the JSON data. ...
  5. Step 5: Transform the JSON data. ...
  6. Step 6: Load the Data into Excel.
Aug 26, 2024

How do I create a JSON file in Windows PowerShell? ›

To generate a JSON string from any object, use the ConvertTo-Json cmdlet. This cmdlet was introduced in PowerShell 3.0. Beginning with PowerShell 6, the cmdlet supports JSON with comments. JSON comments start with two forward slashes ( // ) characters.

How to output JSON in PowerShell? ›

To write JSON to a file in PowerShell, you can use the ConvertTo-Json cmdlet, which converts a PowerShell object into a JSON-formatted string. Once converted, you can then output the string to a file using the Out-File cmdlet.

How do you convert data to JSON format? ›

String data can be easily converted to JSON using the stringify() function, and also it can be done using eval(), which accepts the JavaScript expression that you will learn about in this guide.

How to convert a list data into JSON format? ›

dumps() method from the json library. In this example, we have a list called my_list with a mix of integers and strings. We then use the json. dumps() method to convert the list to a JSON-formatted string, which we store in the json_string variable.

How to convert form data to JSON data? ›

Make a plan: how can we convert form fields to JSON?
  1. Capture the form's submit event and prevent the default submission.
  2. Convert the form's child elements to JSON.
  3. Check to make sure only form field elements are added to the object.
  4. Add a safeguard to only store checkable fields if the checked attribute is set.

How to convert a value to JSON? ›

The JSON.stringify() static method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

How to save output in JSON? ›

Steps to save the json file

Create json file and use "w" (write) mode to save data and files. 4. Use json. dump method to connect file and json data.

How to convert data to string in JSON? ›

Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.

How to convert Excel data into an array? ›

Select cell D9, then press F2 to switch to edit mode. Next, press F9 to convert the cell references to values, which Excel then converts into an array constant.

How to generate a JSON file? ›

Manually creating a JSON file
  1. Open a text editor and enter data in the following Salesforce Analytics schema format: { "fileFormat" : { }, "objects" : [ { <object_detail>, "fields" : [ { <Field1 details> } { <Field2 details> }…… ] }] }
  2. Save and close the file in the . json format.

Top Articles
What Does a Software Developer Do | DeVry University
Credit Card Debt: Guide to Responding to Court Summons
Drury Inn & Suites Bowling Green
El Paso Pet Craigslist
1970 Chevrolet Chevelle SS - Skyway Classics
Aces Fmc Charting
Wfin Local News
Optimal Perks Rs3
Bank Of America Appointments Near Me
Acbl Homeport
Signs Of a Troubled TIPM
Conscious Cloud Dispensary Photos
Condogames Xyz Discord
Fool’s Paradise movie review (2023) | Roger Ebert
Scenes from Paradise: Where to Visit Filming Locations Around the World - Paradise
Napa Autocare Locator
Obsidian Guard's Cutlass
U Break It Near Me
Effingham Bookings Florence Sc
Lawson Uhs
Why Does Lawrence Jones Have Ptsd
Tyler Sis University City
Hobby Stores Near Me Now
Amortization Calculator
Quick Answer: When Is The Zellwood Corn Festival - BikeHike
About My Father Showtimes Near Copper Creek 9
Crossword Help - Find Missing Letters & Solve Clues
Cowboy Pozisyon
Cvs Sport Physicals
Mchoul Funeral Home Of Fishkill Inc. Services
Account Now Login In
Kelley Fliehler Wikipedia
Helloid Worthington Login
Minecraft Jar Google Drive
Umiami Sorority Rankings
Is Arnold Swansinger Married
Tugboat Information
Pensacola Cars Craigslist
Sabrina Scharf Net Worth
Why I’m Joining Flipboard
Craigs List Palm Springs
Express Employment Sign In
Let's co-sleep on it: How I became the mom I swore I'd never be
Craigslist en Santa Cruz, California: Tu Guía Definitiva para Comprar, Vender e Intercambiar - First Republic Craigslist
The best specialist spirits store | Spirituosengalerie Stuttgart
Hazel Moore Boobpedia
How To Customise Mii QR Codes in Tomodachi Life?
Top 1,000 Girl Names for Your Baby Girl in 2024 | Pampers
Stoughton Commuter Rail Schedule
UNC Charlotte Admission Requirements
The Significance Of The Haitian Revolution Was That It Weegy
Invitation Quinceanera Espanol
Latest Posts
Article information

Author: Duane Harber

Last Updated:

Views: 6256

Rating: 4 / 5 (51 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Duane Harber

Birthday: 1999-10-17

Address: Apt. 404 9899 Magnolia Roads, Port Royceville, ID 78186

Phone: +186911129794335

Job: Human Hospitality Planner

Hobby: Listening to music, Orienteering, Knapping, Dance, Mountain biking, Fishing, Pottery

Introduction: My name is Duane Harber, I am a modern, clever, handsome, fair, agreeable, inexpensive, beautiful person who loves writing and wants to share my knowledge and understanding with you.