Quantcast
Channel: Active questions tagged jq - Stack Overflow
Viewing all articles
Browse latest Browse all 582

Parse multilevel json into a csv using jq tool in unix

$
0
0

I have a multilevel json like so:

{"id": "id123","details": {"prod": "prod123","etype": "type1"    },"accounts": [        {"bankName": "bank123","accountType": "account123","openingBalance": "bal123","fromDate": "2023-01-01","toDate": "2023-01-01","missingMonths": [],"transactions": [                {"dateTime": "2020-12-01","description": "a very long string","amount": -599.0,"bal": 8154.83,"type": "Debit"                },                {"dateTime": "2020-12-01","description": "a very long string; a very long string","amount": -4000.0,"balanceAfterTransaction": 4154.83,"type": "Debit"                }            ]        }    ],"accountid": "sample123"}

I want to parse it to a csv in the following format:

id,prod,etype,bankName,accountType,openingBalance,fromDate,toDate,dateTime,description,amount,bal,type,accountidid123,prod123,type1,bank123,account123,bal123,2023-01-01,2023-01-01,2020-12-01,a very long string,-599.0,8154.83,Debit,sample123id123,prod123,type1,bank123,account123,bal123,2023-01-01,2023-01-01,2020-12-01,a very long string; a very long string,-4000.0,4157.83,Debit,sample123

Parse the transactions into a proper csv and repeat the common info for each transaction.

I have gotten only so far yet:

jq --raw-output '[ .id, .details[], .accounts[].transactions[] ]'


Viewing all articles
Browse latest Browse all 582

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>