How to summarize/resume json object in another json object

Viewed 33

How can I summarize a json data inside another json data, that is, put all the key:value ​​inside another json without having to define again.

Ex:

default_headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
    'Content-Type': 'application/json'
}

special_headers = { default_headers, 'Authorization': 'Bearer xxxxxxx'}

# special_headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
#    'Content-Type': 'application/json', 'Authorization': 'Bearer xxxxxxx' }
1 Answers

You can Hash#merge the new hash into the one you already have:

{
  'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36',
  'Content-Type': 'application/json'
}.merge('Authorization': 'Bearer xxxxxxx')
# {:"User-Agent"=>"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36", :"Content-Type"=>"application/json", :Authorization=>"Bearer xxxxxxx"}
Related