You are not logged in.

#1 2019-06-26 18:29:20

mountaineerbr
Banned
Registered: 2018-12-08
Posts: 48

{ SOLVED } Reformatting JSON with jq

I need to reformat a JSON file from this output:


{                                                                                           
  "baseCountry": "US",                                                                                                                                          
  "baseCurrency": "USD",                                                                                                                                        
  "rates": [                                                                                                                            
    {                                                                                                       
      "id": 342,                                                                                                                                        
      "name": "Brazil",                                                                                                                                         
      "name_zh": "巴西",                                                                                                                                        
      "code": "BR",                                                                                                                                               
      "currency_name": "Brazil",                                                                                                                                
      "currency_name_zh": "巴西雷亚尔",                                                                                                                         
      "currency_code": "BRL",                                                                                                                                   
      "rate": 3.84401,                                                                                                                                            
      "hits": 19704,                                                                                                                                              
      "selected": 0,                                                                                                                                              
      "top": 0                                                                                                                                  
    },                                                                                                          
    {                                                                                                       
      "id": 449,                                                                                                                                        
      "name": "Singapore",                                                                                                                                      
      "name_zh": "新加坡",                                                                                                                                      
      "code": "SG",                                                                                                                                               
      "currency_name": "Dollar",                                                                                                                                
      "currency_name_zh": "新币",                                                                                                                               
      "currency_code": "SGD",                                                                                                                                   
      "rate": 1.35289,                                                                                                                                            
      "hits": 1013496,                                                                                                                                            
      "selected": 0,                                                                                                                                              
      "top": 0                                                                                                                                  
    },                                                                                                          
    {                                                                                                       
      "id": 356,                                                                                                                                        
      "name": "China",                                                                                                                                          
      "name_zh": "中国",                                                                                                                                        
      "code": "CN",                                                                                                                                               
      "currency_name": "Yuan Renminbi",                                                                                                                         
      "currency_name_zh": "人民币",                                                                                                                             
      "currency_code": "CNY",                                                                                                                                   
      "rate": 6.8801,                                                                                                                                             
      "hits": 199625,                                                                                                                                             
      "selected": 1,                                                                                                                                              
      "top": 5                                                                                                                                  
    }
  ]
}

If I pass that in jq and try to rearrange with the following equation:

 curl -s https://www.mycurrency.net/US.json | jq -r '[.rates[] | .currency_code as $k | {"\(.currency_code)":"\(.rate)"}]'

, I get this:

[
  {
    "BRL": "3.84401"
  },
  {
    "SGD": "1.35289"
  },
  {
    "CNY": "6.8801"
  }
]

Whereas I need something like this:

[
  {
    "BRL": "3.84401",
    "SGD": "1.35289",
    "CNY": "6.8801"
  }
]

Thanks if somebody can help me out in how to reformat this JSON.
I cannot get those key:value pairs within the same curly brackets (objects of an array[], as far as I understand).
Those many curly brackets cause  returning lots of null values when I try to retrieve values from that !
I am sure it must be something with my jq syntax.

Thanks a lot.

Last edited by mountaineerbr (2019-06-26 19:05:16)

Offline

#2 2019-06-26 18:51:20

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: { SOLVED } Reformatting JSON with jq

I've never been any good with jq, but if you don't need it's flexibility (i.e., if the order of elements in the input is reliable) then the following sed script could do the job:

1 {
	s/.*/[\n   {/p
}
$ {
	s/.*/   }\n]/p
}
/"currency_code":/ {
	s/.*:\([^,]*\),/     \1/
	h
}
/"rate":/ {
	s/.*://
	H
	x
	s/ *\n/:/p
}

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2019-06-26 18:53:56

progandy
Member
Registered: 2012-05-17
Posts: 5,184

Re: { SOLVED } Reformatting JSON with jq

The easiest method is probably to create an array of objects that contain key and value elements. Then convert that to a flattened object with from_entries.

jq -r '[.rates[] | { key: .currency_code, value: .rate } ] | from_entries' </tmp/cur

| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#4 2019-06-26 19:04:06

mountaineerbr
Banned
Registered: 2018-12-08
Posts: 48

Re: { SOLVED } Reformatting JSON with jq

Wow, Progandy!

Thanks a lot for that!

Specially, your explaining in how the process should work with jq rationale..
It is a bit clearer now in my head ..

Also, trilby,  Thanks for the trouble in writing that little script!

Last edited by mountaineerbr (2019-06-26 19:11:31)

Offline

Board footer

Powered by FluxBB