I have the following json file:
{
"test1": {
"POST": "{}",
"GET": "{}"
},
"test2": {
"HEAD": "{}",
"GET": "{}"
}
}
I can convert it to yaml as follows:
yq eval -P input.json --output-format=yaml
which gives me:
test1:
POST: '{}'
GET: '{}'
test2:
HEAD: '{}'
GET: '{}'
Is there an elegant (yq - based way) of:
a) adding a top level mapping element (say named config) as in
config:
test1:
POST: '{}'
GET: '{}'
test2:
HEAD: '{}'
GET: '{}'
b) removing the single quotes from '{}'?
I want my end result to be
config:
test1:
POST: {}
GET: {}
test2:
HEAD: {}
GET: {}
Thanks!
Add a filter to your yq invocation.
{"config": .}adds a wrapping field.[][] |= fromjsoninterprets any value two levels deep as JSON-encoded string.Put together:
Tested with mikefarah/yq v4.35.1
Note: Since v4.18.1, the
eval/ecommand is the default, and can be omitted.