How to delete a Json object from Json File by sed command in BASH


I am having an json file in which a json object need to be deleted. is there any way to delete it by sed command only. my sample file is given below.

{ "name": "ABC", "description": "XYZ", "versions": { }, "json_class": "Chef::Environment", "chef_type": "environment", "default_attributes": { }, "override_attributes": { "company": { "xyz": { "mailer":{ "smtp_host": "Some IP", "imap_host": "Some Host", "imap_user": "Some UserId", "reply_to": "Some User Id", "outbound_user": "", "imap_mail_domain": "compute.oraclecloud.com", "imap_password": ""}, "logicaldomainname": "" } } } }

In this file i want to delete mailer object from the file by sed command. Please take a look and let me know if it is possible by sed command.

my main motive of this deletion is to replace this object with new mailer object. so that next time when another mailer object will be posted than we will append it in xyz json object.

expected output :

{ "name": "ABC", "description": "", "version": { }, "json_class": "Chef::Environment", "chef_type": "environment", "default_attributes": { }, "override_attributes": { "Company": { "XYZ": { "logicaldomainname": "" } } } }


You should strongly consider resolving the licensing issue that prohibits jq:

jq 'del(.override_attributes.company.xyz.mailer) | .description=""' input.json


$ awk -v RS='^$' -v ORS= '{sub(/"mailer":[^}]+},[[:space:]]+/,"")}1' file { "name": "ABC", "description": "", "versions": { }, "json_class": "Chef::Environment", "chef_type": "environment", "default_attributes": { }, "override_attributes": { "Company": { "XYZ": { "logicaldomainname": "" } } } }

The above uses GNU awk for multi-char RS, with other awks you'd build up the record one line at a time and handle it in the END section:

$ awk -v ORS= '{rec=rec $0 RS} END{sub(/"mailer":[^}]+},[[:space:]]+/,"",rec); print rec}' file { "name": "ABC", "description": "", "versions": { }, "json_class": "Chef::Environment", "chef_type": "environment", "default_attributes": { }, "override_attributes": { "Company": { "XYZ": { "logicaldomainname": "" } } } }


Using sed:

sed '/\"mailer\"/,/}/ d; /^$/d' 1.a

"Delete from mailer to the first }, then delete all blank lines (you input has one)".

To overwrite the file ("in-place")

sed -i ...

<em>Note: works for multiline files (as yours).</em>


  • codeigniter email->send being send twice
  • Codeigniter email - browser keeps on loading
  • How to tell if an image has loaded correctly
  • ASP.NET C# - Dropdown list by using User Control
  • Browser-sync in mobile while local development
  • DIRECT replacement of UserHostAddress in ASP.NET Core?
  • Send command to FTP server using sockets in C
  • How to use Google Places API for Web Service in android?
  • Symfony2 Service Container - does get() return objects by reference or copy?
  • nginx, upstream, cors fail
  • Using remote host as working directory, git extensions
  • 'SOCK_RAW' option in 'socket' system call
  • Azure VM Scale Sets Internal Loadbalancer
  • How to configure Joomla 1.7 SMTP email with a google apps email address
  • LG WEBOS : Could not connect to the device, please check the device connection
  • Coherence clustering configuration
  • Specify source IP address for TCP socket when using Linux network device aliases
  • ManagementObjectSearcher select network printers?
  • Send http request through specific network interface
  • How to avoid using dynamic variables / a billion if statements in Java?
  • Google cloud datastore emulator init data
  • PHP - sending email with attachment does not show message content
  • Modifying files nested in tar archive
  • Google OAuth2 for an web application hosted behind NAT (intranet server without public IP)
  • JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
  • Android NFC read Tags issue. Activity starts each time on data received
  • openssl handshake failed
  • How to resolve docker host names (/etc/hosts) in containers
  • Replace and retrieve placeholder value
  • Should I be afraid to use UDP to make a client/server broadcast talk?
  • auth.provider is not set to 'password' when user signs-in with email and password
  • How to concat Pandas dataframe columns
  • how to get username into sql trigger when multiple users signed on from asp membership
  • Cannot resolve symbol 'MyApi'
  • need help with bizarre java.net.HttpURLConnection behavior
  • Authorize attributes not working in MVC 4
  • EntityFramework adding new object to nested object collection
  • Busy indicator not showing up in wpf window [duplicate]
  • Python/Django TangoWithDjango Models and Databases
  • Net Present Value in Excel for Grouped Recurring CF