How to create JSON files from Tabular data with a JSON template

A propos

This howto will show you:

Steps

The tabular source data

In this howto, we will use as source books catalog data that are contained in

You can also used any other tabular data resources.

With the data print command, we can see the content.

tabli data print books.csv@howto
asin            description                                   price   group
-------------   -------------------------------------------   -----   ----------
B007US9NA8      The New Encyclopedia of Modern Bodybuilding   27.18   Sports
1439199191      How To Win Friends And Influence People       27.18   Psychology
9780465050659   The Design of Everyday Things                 9.99    Design
9780465050659   The Design of Everyday Things                 9.99    Psychology
B00555X8OA      Thinking, Fast and Slow                       8.85    Decision
B00555X8OA      Thinking, Fast and Slow                       8.85    Psychology

The JSON template

The JSON template is a template that represents the structure of the JSON resource that should be created that contains variables in the form Create Json From Tabular Data where name should match the column name of the source.

{
  "${group}": {
    "${asin}": {
      "price": "${price}",
      "description": "${description}"
    }
  }
}


where:

  • the variable group matches the group column of the books.csv source
  • the variable asin matches the asin column of the books.csv source
  • the variable price matches the price column of the books.csv source
  • the variable This howto will show you how to create a JSON file from tabular data with the template operation and a JSON template matches the description column of the books.csv source

The template operation

Tabli Data Template Command

By running the below tabli data template operation, we will:

tabli data template --template-selector books_template.json@howto books.csv@howto books.json@temp
Source         Target            Latency (ms)   Row Count   Error   Message
------------   ---------------   ------------   ---------   -----   -------
books@memory   books.json@temp   6              0

The JSON file created

With the data print command, you can see the json file content created.

tabli data print books.json@temp
{
  "Sports": {
    "B007US9NA8": {
      "price": "27.18",
      "description": "The New Encyclopedia of Modern Bodybuilding"
    }
  },
  "Psychology": {
    "1439199191": {
      "price": "27.18",
      "description": "How To Win Friends And Influence People"
    },
    "9780465050659": {
      "price": "9.99",
      "description": "The Design of Everyday Things"
    },
    "B00555X8OA": {
      "price": "8.85",
      "description": "Thinking, Fast and Slow"
    }
  },
  "Design": {
    "9780465050659": {
      "price": "9.99",
      "description": "The Design of Everyday Things"
    }
  },
  "Decision": {
    "B00555X8OA": {
      "price": "8.85",
      "description": "Thinking, Fast and Slow"
    }
  }
}

Task Runner