0

Hy guys, Given an array of JSON objects, I'd like to output a CSV, but in this case i have a different keys and i need to take some values through JSONs' multiple layers.

i'm not skill with a bash script. i have found that link Convert json to csv using jq with different key but I don't know how to adapt it to my case

My Json file:

{
  "status": "success",
  "data": {
    "n_per_page": 10,
    "transactions_by_dealer_code": {
      "06001": [
        {
          "dealer_name": "Pippo",
          "dealer_total_transaction_count": 0,
          "dealer_cancellation_count": 0,
          "dealer_reservation_count": 0,
          "transactons_detail": []
        }
      ],
      "06003": [
        {
          "dealer_name": "Pluto",
          "dealer_total_transaction_count": 1,
          "dealer_cancellation_count": 0,
          "dealer_reservation_count": 1,
          "transactons_detail": [
            {
              "transaction_id": "123456789",
              "deposite_amount": 0.01,
              "vehicle_make": "ferrari"
            }
          ]
        }
      ]
    }
  }
}

the correct output:

06001,Pippo,,,,
06002,Pluto,123456789,0.01,ferrari

Thanks all.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Bombo
  • 137
  • 1
  • 1
  • 10
  • ...and what specific problem did you encounter while doing this yourself? – Charles Duffy Mar 25 '22 at 15:12
  • (if the problem is combining the values from upper layers, `as $varname` to store data from upper layers to use in lower ones is your friend; we have existing Q&A that already describes how to do this) – Charles Duffy Mar 25 '22 at 15:13
  • Note that the two linked duplicates should be used in combination. The former one talks about how to get values from outer layers into the inner ones; the latter talks about how to get your data into CSV form. – Charles Duffy Mar 25 '22 at 15:15
  • I have not found a same question to be duplicated. But my problem is adapt the script in the link I found to my case. – Bombo Mar 25 '22 at 15:21
  • See https://replit.com/@CharlesDuffy2/MetallicGrowingDecompilers#main.sh as an example showing how to apply the linked duplicates to your exact input, to receive your exact output. – Charles Duffy Mar 25 '22 at 17:58
  • To be clear, Stack Overflow's goal is to be a knowledge base of "how to solve this specific technical problem". Our job is to teach the techniques you need to use; it's your job to apply those techniques. If a technique _doesn't work_ when applied, that's a good reason to ask a new question, but if you just haven't tried applying it yet to your own input, that's your job, to be done before asking a question. – Charles Duffy Mar 25 '22 at 18:01
  • You are right, but the problem is, that with my skills i counldn't create an example script, that's why i punt the link, becouse, it was the one that comes closest to my case. Anyway thank you, for creating the script – Bombo Mar 28 '22 at 10:36

0 Answers0