---
url: https://vue-json-excel3.netlify.app/simple-examples.md
description: >-
  Choose which JSON fields to export, label the columns, read nested values and
  download an .xls file.
---

# Simple Example

Pass your rows to `data` and describe the columns with `fields`. Keys are the column labels, values are the (possibly nested) JSON paths to export.

```vue
<script setup>
import JsonExcel from 'vue-json-excel3'

const jsonFields = {
  'Complete name': 'name',
  City: 'city',
  Telephone: 'phone.mobile',
  'Telephone 2': {
    field: 'phone.landline',
    callback: (value) => `Landline Phone - ${value}`,
  },
}

const jsonData = [
  {
    name: 'Tony Peña',
    city: 'New York',
    country: 'United States',
    birthdate: '1978-03-15',
    phone: { mobile: '1-541-754-3010', landline: '(541) 754-3010' },
  },
  {
    name: 'Thessaloniki',
    city: 'Athens',
    country: 'Greece',
    birthdate: '1987-11-23',
    phone: { mobile: '+1 855 275 5071', landline: '(2741) 2621-244' },
  },
]
</script>

<template>
  <JsonExcel
    class="btn"
    :data="jsonData"
    :fields="jsonFields"
    worksheet="My Worksheet"
    name="filename.xls"
  >
    Download Excel (.xls)
  </JsonExcel>
</template>
```

## Fields

`fields` decides which columns are exported. Each value is exported as is, unless you give a `callback` to format it:

```js
const jsonFields = {
  // regular field (exported as is) — nested paths are supported
  fieldLabel: 'attributeName',
  // callback for formatting
  anotherFieldLabel: {
    field: 'anotherAttributeName', // nested paths are supported
    callback: (value) => `formatted value ${value}`,
  },
}
```

If no `fields` are provided, the keys of the first object in `data` are used as the column names.

You can also pass the fields inline with `export-fields`:

```vue-html
<JsonExcel
  :data="jsonData"
  :export-fields="{
    'Human friendly name': '_name_field_from_json',
    'Last name': '_last_name_text',
  }"
/>
```

## Options API

Using the Options API with a globally registered component works the same way:

```js
import { createApp } from 'vue'
import JsonExcel from 'vue-json-excel3'

createApp({
  components: { downloadExcel: JsonExcel },
  data() {
    return {
      json_fields: { 'Complete name': 'name', City: 'city', Telephone: 'phone.mobile' },
      json_data: [
        { name: 'Tony Peña', city: 'New York', phone: { mobile: '1-541-754-3010' } },
      ],
      json_meta: [[{ key: 'charset', value: 'utf-8' }]],
    }
  },
}).mount('#app')
```

```html
<download-excel
  class="btn btn-default"
  :data="json_data"
  :fields="json_fields"
  :meta="json_meta"
  worksheet="My Worksheet"
  name="filename.xls"
>
  Download Excel (you can customize this with html code!)
</download-excel>
```
