JSON remove new lines via Linux and use it as string variable and as param in cURL POST request
Today I had to parse a JSON response in a JUnit test and then reuse it in a cURL command line call. Unfortunately I could not read the JSON response from a file or from an HTTP request, so I had to place it inline as a String variable. My JSON looked like this:
{
"responseHeader": {
"status": 0,
"QTime": 193
},
"defaultCoreName": "collection1",
"initFailures": {},
"status": {
"jet2pilot_shard1_replica2": {
"name": "jet2pilot_shard1_replica2",
"isDefaultCore": false,
"instanceDir": "/opt/solr-4.8.1/example/solr/jet2pilot_shard1_replica2/",
"dataDir": "/opt/solr-4.8.1/example/solr/jet2pilot_shard1_replica2/data/",
"config": "solrconfig.xml",
"schema": "schema.xml",
"startTime": "2014-12-31T19:12:41.633Z",
"uptime": 1350610415
}
}
}
1. Use JSON in a Java string variable
In order to parse JSON from a Java String variable, I had to:
- remove \t,\n,\r characters and
-
escape single and double quotes (',") Thus, Linux shell came to the rescue! So, write your JSON to a file, for example myresponse.json and execute the following command:
cat myresponse.json | tr '\r' ' ' | tr '\n' ' ' | sed "s/[']/\\\'/g" | sed 's/\"/\\"/g' | sed 's/ \{3,\}/ /g' | sed 's/ / /g' > onelinejson.txt
Then you can use JSON as a simple Java String variable like the following:
@Test public void testAktiston(){ String jsonString = "{ \"responseHeader\": { \"status\": 0, \"QTime\": 193 }, \"defaultCoreName\": \"collection1\", \"initFailures\": {}, \"status\": { \"jet2pilot_shard1_replica2\": { \"name\": \"jet2pilot_shard1_replica2\", \"isDefaultCore\": false, \"instanceDir\": \"/opt/solr-4.8.1/example/solr/jet2pilot_shard1_replica2/\", \"dataDir\": \"/opt/solr-4.8.1/example/solr/jet2pilot_shard1_replica2/data/\", \"config\": \"solrconfig.xml\", \"schema\": \"schema.xml\", \"startTime\": \"2014-12-31T19:12:41.633Z\", \"uptime\": 1350610415 } } }"; // test code // parsing and other chaotic code }
2. Use JSON to perform a POST request using cURL
In order to parse JSON from a Java String variable, I had to:
-
remove \t,\n,\r characters and Again, Linux shell makes the world go round! So, write your JSON to a file, for example myresponse.json and execute the following command:
cat myresponse.json | tr '\r' ' ' | tr '\n' ' ' | sed 's/ \{3,\}/ /g' | sed 's/ / /g' > onelinejson.txt
Then use it in your POST request via cURL command (the command is multilined in order to be legible):
curl -v -X POST -H "Cookie: JSESSIONID=00213719A12D07F7E67BE8B580CD9BBC" -H "Content-Type: multipart/form-data" -H "Accept: application/json" -F 'repEx={ "responseHeader": { "status": 0, "QTime": 193 }, "defaultCoreName": "collection1", "initFailures": {}, "status": { "jet2pilot_shard1_replica2": { "name": "jet2pilot_shard1_replica2", "isDefaultCore": false, "instanceDir": "/opt/solr-4.8.1/example/solr/jet2pilot_shard1_replica2/", "dataDir": "/opt/solr-4.8.1/example/solr/jet2pilot_shard1_replica2/data/", "config": "solrconfig.xml", "schema": "schema.xml", "startTime": "2014-12-31T19:12:41.633Z", "uptime": 1350610415 } } }' "http://192.168.1.89:8080/apios/insertios"
That is all about καρντάσια (kardasia is translated as “folks” in Thessaloniki, Greece)! Have a nice year!
Comments