title: How do I parse nested JSON with JQ into CSV-aggregated output?
body: <p>I have a question that is an extension/followup to a previous question I've asked:
  <a href="https://stackoverflow.com/questions/61964573/how-do-i-concatenate-dummy-values-in-jq-based-on-field-value-and-then-csv-aggre">How do I concatenate dummy values in JQ based on field value, and then CSV-aggregate these concatenations?</a></p>
  
  <p>In my bash script, when I run the following jq against my curl result:<br>
  <code>curl -u someKey:someSecret someURL 2&gt;/dev/null | jq -r '.schema' | jq -r -c '.fields'</code></p>
  
  <p>I get back a JSON array as follows:  </p>
  
  <pre><code>[
      {"name":"id", "type":"int"},
      {
          "name": "agents",
          "type": {
              "type": "array",
              "items": {
                  "name": "carSalesAgents",
                  "type": "record"
                  "fields": [
                      {
                          "name": "agentName",
                          "type": ["string", "null"],
                          "default": null
                      },
                      {
                          "name": "agentEmail",
                          "type": ["string", "null"],
                          "default": null
                      },
                      {
                          "name": "agentPhones",
                          "type": {
                              "type": "array",
                              "items": {
                                  "name": "SalesAgentPhone",
                                  "type": "record"
                                  "fields": [
                                      {
                                      "name": "phoneNumber",
                                      "type": "string"
                                      }
                                  ]
                              }
                          },
                          "default": []
                      }
                  ]
              }
          },
          "default": []
      },
      {"name":"description","type":"string"}
  ]
  </code></pre>
  
  <p>Note: line breaks and indentation added here for ease of reading. This is all in reality a single blob of text.</p>
  
  <p>My goal is to do a call with jq applied to return the following, given the example above (again lines and spaces added for readability, but only need to return valid JSON blob):  </p>
  
  <pre><code>{
  "id":1234567890,
  "agents": [
      {
          "agentName": "xxxxxxxxxx",
          "agentEmail": "xxxxxxxxxx",
          "agentPhones": [
              {
                  "phoneNumber": "xxxxxxxxxx"
              },
              {
                  "phoneNumber": "xxxxxxxxxx"
              },
              {
                  "phoneNumber": "xxxxxxxxxx"
              }
          ]
      },
      {
          "agentName": "xxxxxxxxxx",
          "agentEmail": "xxxxxxxxxx",
          "agentPhones": [
              {
                  "phoneNumber": "xxxxxxxxxx"
              },
              {
                  "phoneNumber": "xxxxxxxxxx"
              },
              {
                  "phoneNumber": "xxxxxxxxxx"
              }
          ]
      }
  ],
  "description":"xxxxxxxxxx"
  }
  </code></pre>
  
  <p>To summarise, I am trying to automatically generate templated values that match the "schema" JSON shown above.</p>
  
  <p>So just to clarify, the values for "name" (including their surrounding double-quotes) are concatenated with either:  </p>
  
  <ul>
  <li>:1234567890 ...when the "type" for that object is "int"  </li>
  <li>":xxxxxxxxxx" ...when the "type" for that object is "string"  </li>
  <li>...and when type is "array" or "record" the appropriate enclosures are added {} or [] with the nested content inside.  </li>
  <li>if its an array of records, generate TWO records for the output</li>
  </ul>
  
  <p>The approach I have started down to cater for parsing nested content like this is to have a series of if-then-else's for every combination of each possible jq type.</p>
  
  <p>But this is fast becoming very hard to manage and painful. From my initial scratch efforts...  </p>
  
  <pre><code>echo '[{"name":"id","type":"int"},{"name":"test_string","type":"string"},{"name":"string3ish","type":["string","null"],"default":null}]' | jq -c 'map({(.name): (if .type == "int" then 1234567890 else (if .type == "string" then "xxxxxxxxxx" else (if .type|type == "array" then "xxARRAYxx" else "xxUNKNOWNxx" end) end) end)})|add'
  </code></pre>
  
  <p>I was wondering if anyone knew of a smarter way to do this in bash/shell with JQ.</p>
  
  <p>PS: I have found alternate solutions for such parsing using Java and Python modules, but JQ is preferable for a unique case of limitations around portability. :)</p>
  
  <p>Thanks!</p>