PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, August 26, 2022

[FIXED] How I can transform json to csv with jq?

 August 26, 2022     csv, jq, json, linux     No comments   

Issue

My json file is similar of this:

{
"A1": "1.2"
"A2": "3.5"
"A3": "2.6"
}

I need transform it to csv file and it looks like this:

A1,1.2
A2,3.5
A3,2.6

My code is:

jq -r 'map(.[] | tonumber) | @csv' file.json > file.csv

and my result is:

1.2,3.5,2.6

Solution

Once you fix your example JSON so it's valid:

$ jq -r 'to_entries[] | [.key, (.value | tonumber)] | @csv' input.json
"A1",1.2
"A2",3.5
"A3",2.6

to_entries turns an object into an array of objects with the key field holding the name of one of the original object's keys and value its corresponding value. Then turn each of those objects into a two-element array which is fed to @csv.



Answered By - Shawn
Answer Checked By - Marie Seifert (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing