1

So far I have had no problems converting standard JSON to CSV with jq in the past. But now my data becomes complicated.

Major base tags are account and subaccount. All other values are variables. The entry year got variable values and changed by time very often. That's my task I can't solve.

Here is the output

{
    "jsonrpc": "2.0",
    "result": {
        "current": [{
            "number": 171883808,
            "commission": 10,
            "year": [
                [1999, 9224, 0],
                [2000, 41919, 9224],
                [2001, 162945, 41919],
                [2002, 397993, 162945],
                [2003, 751570, 397993],
                [2004, 886466, 751570]
            ],
            "status": true,
            "last": 9782473,
            "account": "VFUIJOPQW",
            "subaccount": "BLPORDGS"
        }, {
            "number": 69999012,
            "commission": 15,
            "year": [
                [2012, 97587, 0],
                [2013, 472685, 97587],
                [2014, 605963, 472685],
                [2015, 698634, 605963],
                [2016, 1931094, 1745922]
            ],
            "status": true,
            "last": 9782490,
            "account": "VFUIJOXXX",
            "subaccount": "BLPORXXX"
        }],
    "id": 1
    }
}

1 Answer 1

0

I assume you mean to output a table with account, subaccount and the year array. You could do this with the update-assignment operator (|=), e.g.:

jq -r '.result | .current | .[]                 |
       .account    as $account                  |
       .subaccount as $subaccount               |
       .year[] |= ([$account, $subaccount] + .) | 
       .year[] | @tsv'

Output:

VFUIJOPQW  BLPORDGS  1999  9224     0
VFUIJOPQW  BLPORDGS  2000  41919    9224
VFUIJOPQW  BLPORDGS  2001  162945   41919
VFUIJOPQW  BLPORDGS  2002  397993   162945
VFUIJOPQW  BLPORDGS  2003  751570   397993
VFUIJOPQW  BLPORDGS  2004  886466   751570
VFUIJOXXX  BLPORXXX  2012  97587    0
VFUIJOXXX  BLPORXXX  2013  472685   97587
VFUIJOXXX  BLPORXXX  2014  605963   472685
VFUIJOXXX  BLPORXXX  2015  698634   605963
VFUIJOXXX  BLPORXXX  2016  1931094  1745922
2
  • that really helped me. How could I do this if I have different accounts. I have edited my post. Commented May 17, 2021 at 12:40
  • @safect: Updated to a cleaner version that supports multiple element arrays Commented May 17, 2021 at 14:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.