Da JSON a YAML da riga di comando

Comandi di una riga per yq, Python, Node.js e PowerShell, in entrambe le direzioni.

Opzioni di output

JSONinput

Vuoto

YAMLoutput

L'output YAML appare qui

Funziona interamente nel tuo browser: nulla viene caricato. Si applicano limiti di uso corretto (5 MB per input, numero di conversioni limitato) per prevenire spam e bot.

Carica da URL

Il file viene recuperato direttamente dal tuo browser, quindi il server deve consentire le richieste cross-origin.

Scorciatoie da tastiera

Ctrl / ⌘ + Enter Converti ora
Ctrl / ⌘ + S Scarica l'output
Alt + Shift + C Copia l'output
Alt + Shift + F Ripara e formatta il JSON di input
Alt + Shift + S Inverti la direzione
Alt + Shift + T Passa tra le viste codice, albero e mappa
Ctrl / ⌘ + Z Annulla, anche dopo Formatta, Ripara e Cancella
Esc Esci da schermo intero
Dimensione del testo nell'editor

yq: la via più rapida in qualsiasi shell

mikefarah/yq è un singolo binario per Linux, macOS e Windows che legge e scrive JSON e YAML.

bash · zsh · PowerShell
# JSON to YAML
yq -p json -o yaml '.' data.json > data.yaml

# YAML to JSON
yq -o json '.' data.yaml > data.json

# Multi-document YAML to one JSON array
yq ea -o json '[.]' manifests.yaml > manifests.json
Conversione in blocco e interrogazioni
# Convert every .json file in the folder
for f in *.json; do
  yq -p json -o yaml '.' "$f" > "${f%.json}.yaml"
done

# Query YAML with jq by converting on the fly
yq -o json '.' deployment.yaml | jq '.spec.template.spec.containers[].image'

PowerShell su Windows

PowerShell 7 supporta JSON nativamente; il modulo powershell-yaml aggiunge YAML in entrambe le direzioni. Sposta -Depth per vedere perché i dati annidati ne hanno bisogno.

PowerShell 7
# One-time setup
Install-Module powershell-yaml -Scope CurrentUser

# JSON to YAML
Get-Content data.json -Raw | ConvertFrom-Json -AsHashtable | ConvertTo-Yaml | Set-Content data.yaml

# YAML to JSON (raise -Depth, the default of 2 truncates nested data)
Get-Content data.yaml -Raw | ConvertFrom-Yaml | ConvertTo-Json -Depth 100 | Set-Content data.json

Node.js

js-yaml include una CLI per passare da YAML a JSON; il contrario è uno script di una riga.

bash
# YAML to JSON with the js-yaml CLI
npx js-yaml data.yaml > data.json

# JSON to YAML (npm install js-yaml first)
node -e 'const y = require("js-yaml"); process.stdout.write(y.dump(JSON.parse(require("fs").readFileSync(0, "utf8"))))' < data.json > data.yaml

Python, Ruby e kubectl

Hai già installato uno di questi? Forse non ti serve altro.

bash
# Python (pip install pyyaml)
python3 -c 'import sys, json, yaml; yaml.safe_dump(json.load(sys.stdin), sys.stdout, sort_keys=False)' < data.json > data.yaml

# Ruby (standard library only)
ruby -ryaml -rjson -e 'puts YAML.dump(JSON.parse(STDIN.read))' < data.json > data.yaml

# kubectl: print any manifest file as YAML or JSON
kubectl apply -f web.json --dry-run=client -o yaml

Altre opzioni Python, inclusi i file multi-documento e ruamel.yaml, sono nella guida Python.

Altri strumenti

Convertitori, validatori e guide passo passo per ogni punto in cui JSON e YAML si incontrano.

Domande frequenti

Come converto JSON in YAML da riga di comando?

Con yq: yq -p json -o yaml '.' data.json > data.yaml. Senza yq, usa il comando di una riga in Python con PyYAML, la libreria standard di Ruby o il modulo powershell-yaml su Windows.

Come converto YAML in JSON in bash?

Esegui yq -o json '.' data.yaml > data.json. Se il file contiene più documenti, usa yq ea -o json '[.]' file.yaml per raccoglierli in un unico array JSON.

Quale yq devo installare?

Questi esempi usano mikefarah/yq (Go, un singolo binario, installabile con brew, apt, snap, choco o winget). Il pacchetto Python chiamato anch'esso yq è un wrapper di jq con flag diversi: yq -y . data.json produce YAML e yq . data.yaml produce JSON.

jq può leggere YAML?

No, jq legge solo JSON. Converti prima lo YAML, ad esempio con yq -o json '.' file.yaml | jq '.spec', oppure usa direttamente sullo YAML le espressioni in stile jq di yq.

Perché ConvertTo-Json di PowerShell taglia i miei dati?

Per impostazione predefinita ConvertTo-Json serializza solo due livelli di profondità e sostituisce tutto ciò che è più profondo con un nome di tipo. Passa sempre -Depth, ad esempio -Depth 100, quando converti configurazioni annidate.