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

jq parse string array and string object

$
0
0

I have this json string:

json_string="{\"emailRecipients\":\"['test@example.com', 'tes2t@example.com', 'test3@example.com']\", \"anotherEmails\":\"{'test1': 'test1@example.com', 'test2': 'test2@example.com', 'test3': 'test3@example.com'}\", \"some_key\": 3, \"another_key\": \"some_value\"}"

I'm trying to use jq to parse the emailRecipients as a json array and anotherEmails as a json object.

I'm trying to make a command that can work with any key, not emailRecipients just and anotherEmails. I also need to keep keys-values that don't have json objects/arrays inside intact.

So I tried to do this:

parsed_json=$(echo "$json_string" | jq 'with_entries(    .value |= if startswith("\"[") and endswith("\"]") then        gsub("''";"\"") | fromjson    else        .    end)')

But I don't know why the gsub isn't working. Any idea of what I'm doing wrong ?


Viewing all articles
Browse latest Browse all 657

Trending Articles



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