Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 612

How to assign field name in jq based on a try catch?

$
0
0

I have a json like this:

{"a.json": "....",    ....}

The "..." can be either a json string (The fromjson can decode it then) or it is something else, then it fails.

I want a jq filter that outputs if json was found in the string like "{}"

{"a.json": {},   ....}

Or else if it was a string like "shh" then it removes the ".json" from the field name:

{"a": "shh",    ....}

There can be other fields in the object, keep them in place. Find all fields ending with .json (like the a.json) and do this operation for all of them! Thanks

When:

with_entries(        select(.key|test("\.json$")) as $root            | $root.value = $root.value as $sub | ($sub|try fromjson catch $sub))

This works for the value, but I cannot find a way to chnge the .key in the catch block.


Viewing all articles
Browse latest Browse all 612

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>