4 ms·
Well, there's this option without the blacklist: jq -r ' tostream | select(length > 1) | (.[0] | map("[" + @json + "]") | join(""))
by jolmg 7y ago
Well, there's this option without the blacklist:
jq -r '
tostream
| select(length > 1)
| (.[0] | map("[" + @json + "]") | join(""))
+ " = " + (.[1] | @json)
'
And this other option with the blacklist patterns:
jq -r '
tostream
| select(length > 1)
| (
.[0] | map(
if type == "number" or (tostring | test("[@-]|^[0-9]|^else$"))
then "[" + @json + "]"
else "." + .
end
) | join("")
) + " = " + (.[1] | @json)
'
(The blacklist here is non-exhaustive, but an example.)
- jolmg 7y agoIt occurs to me that a better way to blacklist would be something like: jq -r ' tostream | select(length > 1) | ( .[0] | map( if tostring | ( test("^[A-Za-z$_][0-9A-Za-z$_]*$") and ( . as $property | ["if", "else"] | all(. != $property) ) ) then "." + . else "[" + @json + "]" end ) | join("") ) + " = " + (.[1] | @json) ' You whitelist against what the syntax allows for identifiers and then you blacklist reserved keywords. Writing it this way makes it easier to verify for correctness when comparing with the ECMAScript Specs. This is still a non-exhaustive blacklist and the whitelist regex lacks allowed unicode characters.