0

Lets say I have an I/P json file as below. And I want to extract the O/P in a CSV format with the below fields. Specifically, I want to get the value of the key "Gamma" in the o/p if the key "Gamma" exists in "tags" map. If the key doesn't exists, it should just print a NULL value. The expected o/p is below.

generated_time,platform,id,,
2021-09-09:12:03:12,earth,2eeee67748,Ray,2021-08-25 09:41:06
2021-09-09:12:03:12,sun,xxxxx12334,NULL,2021-08-25 10:11:31

[
  { 
    "generated_time": "generated_time",
    "platform": "platform",
    "id": "id"
  },
    {
    "generated_time": "2021-09-09:12:03:12",
    "platform": "earth",
    "id": "2eeee67748",
    "tags": {
      "app": "map",
      "Gamma": "Ray",
      "null": [
        "allow-all-humans"
      ]
          },
    "created": "2021-08-25 09:41:06"
  },
   { 
    "generated_time": "2021-09-09:12:03:12",
    "platform": "sun",
    "id": "xxxxx12334",
    "tags": {
      "component": "machine",
      "environment": "hot",
      "null": [
        "aallow-all-humans"
      ]
    },
    "created": "2021-08-25 10:11:31"
   }
]

5
  • 1
    sounds easy, what's the problem? Commented Sep 9, 2021 at 22:44
  • I can get the simple one using jq -r '.[] | [.generated_time,.platform,.id,.created,.tags.Gamma] | join(",")' but looking for efficient way to get an explicit NULL Commented Sep 10, 2021 at 2:16
  • Why is the first object different? Do you want to exclude that one from the result? Commented Sep 10, 2021 at 10:13
  • I would just print nothing for the first object since it doesn't have the tags map. Commented Sep 10, 2021 at 13:12
  • @GarrGodfrey, do you have anything in mind? Commented Sep 10, 2021 at 14:12

1 Answer 1

1

jq has a builtin @csv which renders an array

as CSV with double quotes for strings, and quotes escaped by repetition.

If the additional quoting (as compared to your expected output) isn't an issue, the following

jq --raw-output '
  
  # produce an array for each element in the input array
  .[] | [
    
    # containing the first three columns unchanged
    .generated_time, .platform, .id,
    
    # if the input element has a field named "tags"
    if has("tags")
    
    # then add two more columns and replace an inexistant Gamma with "NULL"
    then (.tags.Gamma // "NULL", .created)
    
    # otherwise add two empty columns instead
    else (null, null) end
    
  # and convert the array into CSV format
  ] | @csv
  
' input.json

will produce

"generated_time","platform","id",,
"2021-09-09:12:03:12","earth","2eeee67748","Ray","2021-08-25 09:41:06"
"2021-09-09:12:03:12","sun","xxxxx12334","NULL","2021-08-25 10:11:31"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.