JSON Data – How to Transform Integer to Dollar Amount Using jq

jqjson

I have data like this

{
  "dateCreated": "2020-06-12",
  "status": "pending",
  "depositDate": "2020-06-15",
  "amount": 41237
}
{
  "dateCreated": "2020-06-05",
  "status": "paid",
  "depositDate": "2020-06-08",
  "amount": 37839
}
{
  "dateCreated": "2020-04-02",
  "status": "paid",
  "depositDate": "2020-04-03",
  "amount": 67
}

that's being formatted with jq like so:

 request-some-api | jq '.data[] | {dateCreated: .created | strftime("%Y-%m-%d"), status: .status, depositDate: .arrival_date | strftime("%Y-%m-%d"), amount: .amount,}'

and I'd like to modify the .amount so that it displays values rather than the number of cents as a dollar amount with a decimal place…

{
  "dateCreated": "2020-06-12",
  "status": "pending",
  "depositDate": "2020-06-15",
  "amount": $412.37
}
{
  "dateCreated": "2020-06-05",
  "status": "paid",
  "depositDate": "2020-06-08",
  "amount": $378.39
}
{
  "dateCreated": "2020-04-02",
  "status": "paid",
  "depositDate": "2020-04-03",
  "amount": $.67
}

…but I haven't found any documentation about this? Is jq able to do this conversion? Even without the $ sign but just adding the . between dollars and cents would be helpful.

Best Answer

Like this:

jq '.amount = "$" + (.amount/100|tostring)' file.json

Output

{
  "dateCreated": "2020-06-12",
  "status": "pending",
  "depositDate": "2020-06-15",
  "amount": "$412.37"
}
{
  "dateCreated": "2020-06-05",
  "status": "paid",
  "depositDate": "2020-06-08",
  "amount": "$378.39"
}
{
  "dateCreated": "2020-04-02",
  "status": "paid",
  "depositDate": "2020-04-03",
  "amount": "$0.67"
}
Related Question