I’m trying to identify object elements which a key starting with @t. My goal is to delete them from the object all together.
Example Input
{
"process_state": {
"@user_id": "john smith",
"@t39ee396f50": 1,
"@t375b0311e8": 1,
"@t12dd92bf45": 1
}
}
Expected Output
{
"process_state": {
"@user_id": "john smith",
}
}
I’ve tried using jq and jtc to accomplish this and both seem to struggle with the leading @ symbol. I’m assuming it’s a format issue with my code. Can I use wildcards? I’ve tried a couple methods with no luck.
JQ
jq ‘. |= map(select(. | contains("@t") | not))’
Error: and string ("@t") cannot have their containment checked
JTC
<file jtc -w'<process_state.@t*>l:’
No error but @t* fields still exist in json object.
Any help is much appreciated.
>Solution :
We’re going to use test("^@t") instead of contains("@t") since we want to check for a leading @t. But that’s not why you are getting that error. You have the wrong object on the left-hand side of |=, and map doesn’t do what you think for objects.
Two solutions:
-
Using filtering (like you were attempting):
.process_state |= with_entries( select( .key | test("^@t") | not ) )Demo on jqplay
-
Using deletion:
.process_state |= del( .[ keys_unsorted[] | select( test("^@t") ) ] )Demo on jqplay