2

I have a large dump of data in json that looks like:

[{
   "recordList" : {
      "record" : [{
          "Production" : {
              "creator" : {
                  "name" : "A"
              }
          }
      },
      {
          "Production" : {}
      },
      {
          "Production" : [{
              "creator" : {
                  "name" : "B"
              },
              "creator" : {
                  "name" : "C"
              }
              }]
          }]
      }
}]

I need to check if there is at least one creator in a record or not. If there is I give a 1 else a 0 for that field in a CSV-file.

My code:

jq -r '.[].recordList.record[]|"\(if ((.Production.creator.name)? // (.Production[]?.creator.name)?) == null or ((.Production.creator.name)?|length // (.Production[]?.creator.name)?|length) == 0 then 0 else 1 end),"' file.json

The problem is that the field 'Production' is only an array when there are multiple creators.

The result I want to get in this case is:

1,
0,
1,
Tine
  • 35
  • 4
  • The third one is odd, as it specifies an object with duplicate `creator` keys. Is it really a list containing a single object, or is it really something like `"Production": [{}, {}]`? – chepner Mar 21 '18 at 15:12

2 Answers2

1

jq solution:

jq -r '.[].recordList.record[].Production 
       | "\(if ((type == "array" and .[0].creator.name !="") 
                 or (type == "object" and .creator.name and .creator.name !="")) 
            then 1 else 0 end),"' file.json

The output:

1,
0,
1,
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
  • This works indeed, thanks! But it is also possible that there is a creator field that's empty. In that case it also has to give a 0 and not a 1. I didn't mention that before. For example when the first creator has name "" instead of "A", then the output should be: 0, 0, 1, – Tine Mar 22 '18 at 08:09
  • Still have another question ... What if "creator" is for example "creator.something"? I thought I had to work with .[0]["creator.something"].name for an array and ["creator.something"].name for an object, but that doesn't work. The error I get is: Cannot index array with string "name". Thanks in advance! – Tine Mar 22 '18 at 10:11
0

Simplified jq solution:

jq -r '.[].recordList.record[].Production 
   | ((type == "array" and .[0].creator.name) or .creator.name) 
   | if . then "1," else "0," end' file.json
peak
  • 105,803
  • 17
  • 152
  • 177
  • This works indeed, thanks! But it is also possible that there is a creator field that's empty. In that case it also has to give a 0 and not a 1. I didn't mention that before. For example when the first creator has name "" instead of "A", then the output should be: 0, 0, 1, – Tine Mar 22 '18 at 08:13