skip to Main Content

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


  1. 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"
    
    Login or Signup to reply.
  2. fromjson allows you to parse a JSON-string into the corresponding JSON entity.

    e.g.:

    $ jq 'fromjson' <<EOF
    "{"a":1}"
    EOF
    {
      "a": 1
    }
    

    Integrating into your filter:

    jq <my.json -r '.getFeedFromMyTeam[] | .recipients[] as $t
    | [.OpenedAt,$t.receiverId,$t.receiverName,.senderId,(.feedData|fromjson.keyTwo)]
    | @csv' 
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search