admin管理员组文章数量:1435859
Im trying to write a JQ function that will take some key/value arguments and format them into a nested object of various data types.
- If the arg key has a period, then use the period as a delimiter for nested object keys. Create that object and place the value there.
- If the value has commas in it, then assume those are array value delimiters, and create the array.
Examples:
--arg foo.bar baz
would create{"foo":{"bar":"baz"}}
--arg numbers.prime 2,3,5,7,11,13
would create{"numbers":{"prime":[2,3,5,7,11,13]}}
--arg someint 123 would create {"someint: 123}
Content of args2json.jq:
# Cast some text into the most likely value and appropriate data type
def cast_arg:
. as $val |
if $val == "null" then null
elif $val == "true" then true
elif $val == "false" then false
else ( $val
| try tonumber catch ($val
| split(",") as $segments
| if ($segments|length) == 0 then null
elif ($segments|length) == 1 then $val
else ($segments|map(cast_arg))
end
)
) end;
# Take the --arg foo.bar baz,bang,qux into {foo: {bar: [baz, bang, qux]}}
def args2json:
. as $params
| to_entries
| reduce .[] as $entrties ([];
$entrties
| (setpath(.key | split("."); .value | cast_arg) | del(.key) | del(.value) | $params + . )
);
.params=($ARGS.named | args2json)
Execution:
jq --null-input \
--arg count 5 \
--arg name john \
--arg enabled false \
--arg shouldbenull null \
--arg mamals pig,cat,dog \
--arg sea.creatures 'shark,crab,654,squid' \
--from-file ./jq/modifiers/args2json.jq
I would expect to see the output:
{
"count": 5,
"name": "john",
"enabled": false,
"shouldbenull": null,
"mamals": [
"pig",
"cat",
"dog"
],
"sea": {
"creatures": [
"shark",
"crab",
654,
"squid"
]
}
}
But here's the actual output
{
"params": {
"count": "5",
"name": "john",
"enabled": "false",
"shouldbenull": "null",
"mamals": "pig,cat,dog",
"sea.creatures": "shark,crab,654,squid",
"sea": {
"creatures": [
"shark",
"crab",
654,
"squid"
]
}
}
}
The last {sea: creatures: [...]}}
looks great. All values got casted properly and the creatures array is nested inside the sea object property. But it only seems to update the very last thing in the output and nothing else which I can't figure out.
Any help would be appreciated
Im trying to write a JQ function that will take some key/value arguments and format them into a nested object of various data types.
- If the arg key has a period, then use the period as a delimiter for nested object keys. Create that object and place the value there.
- If the value has commas in it, then assume those are array value delimiters, and create the array.
Examples:
--arg foo.bar baz
would create{"foo":{"bar":"baz"}}
--arg numbers.prime 2,3,5,7,11,13
would create{"numbers":{"prime":[2,3,5,7,11,13]}}
--arg someint 123 would create {"someint: 123}
Content of args2json.jq:
# Cast some text into the most likely value and appropriate data type
def cast_arg:
. as $val |
if $val == "null" then null
elif $val == "true" then true
elif $val == "false" then false
else ( $val
| try tonumber catch ($val
| split(",") as $segments
| if ($segments|length) == 0 then null
elif ($segments|length) == 1 then $val
else ($segments|map(cast_arg))
end
)
) end;
# Take the --arg foo.bar baz,bang,qux into {foo: {bar: [baz, bang, qux]}}
def args2json:
. as $params
| to_entries
| reduce .[] as $entrties ([];
$entrties
| (setpath(.key | split("."); .value | cast_arg) | del(.key) | del(.value) | $params + . )
);
.params=($ARGS.named | args2json)
Execution:
jq --null-input \
--arg count 5 \
--arg name john \
--arg enabled false \
--arg shouldbenull null \
--arg mamals pig,cat,dog \
--arg sea.creatures 'shark,crab,654,squid' \
--from-file ./jq/modifiers/args2json.jq
I would expect to see the output:
{
"count": 5,
"name": "john",
"enabled": false,
"shouldbenull": null,
"mamals": [
"pig",
"cat",
"dog"
],
"sea": {
"creatures": [
"shark",
"crab",
654,
"squid"
]
}
}
But here's the actual output
{
"params": {
"count": "5",
"name": "john",
"enabled": "false",
"shouldbenull": "null",
"mamals": "pig,cat,dog",
"sea.creatures": "shark,crab,654,squid",
"sea": {
"creatures": [
"shark",
"crab",
654,
"squid"
]
}
}
}
The last {sea: creatures: [...]}}
looks great. All values got casted properly and the creatures array is nested inside the sea object property. But it only seems to update the very last thing in the output and nothing else which I can't figure out.
Any help would be appreciated
Share Improve this question asked Nov 16, 2024 at 14:20 J HJ H 531 silver badge7 bronze badges1 Answer
Reset to default 2This should work:
def args2json:
reduce to_entries[] as $e (null;
setpath($e.key | split("."); $e.value | cast_arg)
);
本文标签: Issue creating nested object in jqStack Overflow
版权声明:本文标题:Issue creating nested object in jq - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745657075a2668770.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论