# tomq
**tomq** is a simple tool that converts TOML to JSON and JSON to TOML.
## Table of Contents
- [Installation](#installation)
- [Examples](#examples)
- [TOML to JSON](#toml-to-json)
- [jq filters](#jq-filtering)
- [Re-transcode to TOML](#re-trasconding-to-toml)
- [JSON to TOML](#converting-json-to-toml)
- [Handling `null` values](#handling-null-values)
- [Fallback key](#providing-a-fallback-key)
## Installation
#### Nix and NixOS
##### Release
```shell
nix-env -f https://gitlab.com/Kores/tomq/-/archive/master/tomq-master.tar.bz2 -i tomq
```
##### Development
```shell
nix-env -f https://gitlab.com/Kores/tomq/-/archive/master/tomq-master.tar.bz2 -i tomq-bleeding
```
##### Additional packages
You may also want to install `jq` and `bat`:
```shell
nix-env -iA nixpkgs.jq nixpkgs.bat
```
They are not required in order to convert from TOML to JSON and vice versa,
but **jq** is required for filtering and **bat** is required for the `--bat` option.
##### Shell
You can easily try **tomq** with **jq** and **bat** using `nix-shell`:
```shell
nix-shell https://gitlab.com/Kores/tomq/-/archive/shell/tomq-shell.tar.bz2
```
#### Cargo
##### Release
```shell
cargo install tomq
```
##### Development
```shell
cargo install --git https://gitlab.com/Kores/tomq.git
```
#### Other distros
**tomq** was recently released, I'm still cleaning up the code and haven't had time to package it for other
distros. Once I feel comfortable, I will first submit it to [nixpkgs](https://github.com/NixOS/nixpkgs) and then
start packaging it for other distros.
## Examples
### TOML to JSON
*sh (and compatibles, like tcsh/csh/zsh/bash):*
```shell
description = "tomq is extraordinary"
EOF
```
*fish:*
```fish
printf %s\n 'title = "tomq example"' 'description = "tomq is extraordinary"' | tomq
```
*nushell:*
```nu
'
title = "tomq example"
description = "tomq is extraordinary"
#### Extra
Converts all `Cargo.toml` files to JSON format by streaming the contents from the files
and piping to **jq** on demand.
```shell
### jq filtering
**requires `jq` binary to be installed and available in the PATH (or any default location)**
Applies a jq filter and outputs the JSON format.
```shell
tomq '.package' Cargo.toml
```
### *Re-trasconding* to TOML
```shell
tomq -R '.package' Cargo.toml
```
### Converting JSON to TOML
```shell
#### Converting JSON to TOML (multi-document)
Converts from JSON to TOML using multi-document extension (non TOML spec compliant).
```shell
> This is not compliant to the TOML specification, but preserves the original JSON structure.
#### Converting JSON to TOML (common key)
Converts from JSON to TOML using a common key (TOML spec compliant).
```shell
##### Note
This is compliant to the TOML specification, but changes the JSON structure, which can be
recovered using **jq**:
```shell
### Handling `null` values
Turn `null` values into empty documents:
```shell
#### Note
And even with the fact that **tomq** *moslty* preserves the original JSON structure,
TOML doesn't have a sense of null values, so turning the previous output back to JSON
will result in an empty object:
```shell
{
"name": "tomq"
}
{
"name": "tomq2"
}
null
```
```shell
name = "tomq"
---
name = "tomq2"
---
```
```shell
{
"name": "tomq"
}
{
"name": "tomq2"
}
{}⏎
```
### Providing a fallback key
For cases where the JSON output is not an object:
```shell
"tomq"
"tomq2"
```
```shell
names = "tomq"
---
names = "tomq2"
```
In case there are `null` values, TOML can't represent them, so you will need the `-E` flag:
```shell
"tomq"
"tomq2"
null
````
```shell
names = "tomq"
---
names = "tomq2"
---
````
`-U` may also be handy in those cases:
```shell
[[names]]
value = "tomq"
[[names]]
value = "tomq2"
```
```shell
"tomq"
"tomq2"
```