Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Extracting data from nested JSON with escape backslash characters by using jq

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

"{""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.

>Solution :

feedData is a JSON object, so you can convert it with fromjson:

$ jq -r '.getFeedFromMyTeam[] | .recipients[] as $t | [.OpenedAt,$t.receiverId,$t.receiverName,.senderId,(.feedData | fromjson | .keyTwo)] | @csv' file.json
1693321311,"johns","John Smith","janed","my_data_two"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading