Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace String in a JSON file

"cassandra": {
    "host": "1.1.1.1",
    "portNo": "9042",
    "keyspace": "good"
},
"postgres": {
    "host": "2.2.2.2",
    "portNo": "5432",
    "database": "dude",
    "username": "root",
    "password": "something"
}

This is a part of the json file. I need to edit the hosts part with two different IPs using a script. Anyone have any idea

like image 870
SANITH Avatar asked Sep 13 '26 01:09

SANITH


1 Answers

For proper json (your data above surrounded by {}) using jq:

$ jq '.|.cassandra.host="foo"' file.json
{
  "cassandra": {
    "host": "foo",
    "portNo": "9042",
    "keyspace": "good"
  },
  "postgres": {
    "host": "2.2.2.2",
    "portNo": "5432",
    "database": "dude",
    "username": "root",
    "password": "something"
  }
}
like image 139
James Brown Avatar answered Sep 14 '26 13:09

James Brown