I have a non-standard format of JSON which is using some nested data with backslash escape characters.
{
"getFeedFromMyTeam": [
{
"OpenedAt": 1693321311,
"feedData": "{"keyOne":"my_data_one","keyTwo":"my_data_two"}",
"feedId": "123456789",
"recipients": [
{
"receiverId": "johns",
"receiverName": "John Smith"
}
],
"senderId": "janed",
"senderName": "Jane Doe"
}
],
"hasMore": true,
"paginationToken": "100"
}
I’m trying to convert it to a CSV file with this comand:
cat my.json | jq -r '.getFeedFromMyTeam[] | .recipients[] as $t | [.OpenedAt,$t.receiverId,$t.receiverName,.senderId,.feedData] | @csv'
All is good however instead of full ".feedData" which now looks like:
"{""keyOne"":""my_data_one"",""keyTwo"":""my_data_two""}"
I would like to get only data from "keyTwo" which is "my_data_two"
Now it is:
1693321311,"johns","John Smith","janed","{""keyOne"":""my_data_one"",""keyTwo"":""my_data_two""}"
I would like to get:
1693321311,"johns","John Smith","janed","my_data_two"
Ideally I would like to use only jq but if not possible then maybe also awk, sed etc.
2
Answers
feedData
is a JSON object, so you can convert it withfromjson
:fromjson
allows you to parse a JSON-string into the corresponding JSON entity.e.g.:
Integrating into your filter: