On windows,I am try to edit my own json file
I use Enter key to get a new line. when I use git bash with command curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' --data-binary #act.json
it just throw some error action_request_validation_exception ,and i use \n on the fisrt line,it still doesn't work,then I use data generated from JSON Generator my git bash can read it well.I read some resource, it is related to new line.so here comes my question,when I want to edit my own json file,how to make a new line just like the genrated file from json generator .thanks a lot!
Related
I have a bash script which sends curl post requests. I want to pass data as bash script parameters. However one of the parameter has spaces in the string and it fails with the error below.
Error parsing JSON data.\n\tString not terminated on line
In shell script, I'm sending an argument like this format {"name":"'$2'"}
Could you please help me to solve that issue?
Thanks
jq is good not only for manipulating existing JSON data, but creating new data, as it does things like correctly handling characters that can't appear unescaped in JSON strings, and proper quoting. Something like
curl ... -d"$(jq -n --arg val "$2" '{name: $val}')"
It would be better if you add enough data while asking question.
I assume your json will be like
{
"name": "argument passed"
}
curl -XPOST "your/url/here" -H 'Content-Type:application/json' -d'{"name":"'$1'"}'
Save the above command as post_request.sh(Feel free to change the name).
Run using below comand.
sh post_request.sh "argument passed"
"argument passed" will be your name with space.
Following is the sample output data in json format when i create a JIRA from command line using curl command.
{"id":"123456","key":"ABCD-123","self":"http://abcd.com/rest/api/2/issue/123456"}
How can i read ABCD-1234 into a variable. i have tried json data parser jq but it didn't help.
Reference Link: https://developer.atlassian.com/server/jira/platform/jira-rest-api-examples/
With jq:
key=$(jq -r '.key' file)
I am using the following tool: https://github.com/EnigmaBridge/certbot-external-auth#json-mode
The tool returns a JSON object and waits for \n before it continues.
What I would like to achieve is this:
Grab the first JSON object JSONOUTPUT1=$(certbot ...)
Process the JSON output
Send the \n input to the certbot command in JSONOUPUT1 (see point 1)
Collect the next JSON object in JSONOUTPUT2
I'm not sure where to start to achieve this, any help is welcome.
I am guessing you want an '\n' right after your certbot terminates:
JSONOUTPUT1=$(certbot ... && echo)
I am getting response from the below command in JSON format in .sh file (using shell script):
**curl https://api.flipkart.net/sellers/skus/$col1/listings -H "Content-Type: application/json" -H "Authorization: Bearer $value"**
Here is the response I am getting:
{"listingId":"LSMSW","skuId":"M7489","fsn":"ACCCQD","attributeValues":{"actual_stock_count":"1","mrp":"199","seller_listing_state":"current","procurement_sla":"2","zonal_shipping_charge":"0","stock_count":"10","local_shipping_charge":"0","listing_status":"ACTIVE","max_order_quantity_allowed":"3","fulfilled_by":"seller","fk_release_date":"2016-08-29 10:00:08","selling_price":"529","inventory_count":"10","national_shipping_charge":"0","sku_id":"M7489"},"listingValidations":null}
Now I want to extract all the value in a separate variable for reuse it
like:
LISTINGID='listing id value'
SKUID='skuId value'
and many more.
If anyone know the answer please comment with explanation.
Here is a solution using jq. If data.json contains the sample data then the following bash script
#!/bin/bash
jq -M -r '
.listingId
, .skuId
, .attributeValues.mrp
' data.json | \
while read -r listingId
read -r skuId
read -r mrp; do
echo "$listingId $skuId $mrp"
done
produces
LSMSW M7489 199
The jq command in the script writes out the specified attributes on separate lines which are read into bash variables by the successive read -r commands. It should be clear how to extend or modify this to meet your requirements – the primary constraint is that for each line written by jq there should be a corresponding read -r.
I am not aware of any bash json parsers. I would go with Python or Php and parse the Json string in one of these languages and export values from there. In Php there is the putenv() method.
Good luck!
Edit:
Check here
Parsing JSON with Unix tools
when copying a database from one host to another I get the folowing error : Missing JSON list of 'docs'
Here is what I do :
source> curl -X GET http://127.0.0.1:5984/cozy/_all_docs?include_docs=true > cozy.dump
destination> curl -X PUT http://127.0.0.1:5984/cozy
{"ok":true}
destination> curl -d #cozy.dump -H "Content-type: application/json" -X POST http://localhost:5984/cozy/_bulk_docs
{"error":"bad_request","reason":"Missing JSON list of 'docs'"}
any idea ?
Thanks !
This is, indeed, a problem with versions.
Fortunately it is fairly easy to fix: just change the first line in the dump, eg.
{"total_rows": 8244, "offset": 0, "rows": [
to
{"docs": [
The dumps can now be used in the later versions.
I know this is an old question but I am still posting an answer in case some one else is looking for the solution. The bulk docs api accepts the request in a certain form.
{docs:[{},{},{}]}
The docs key must contain an array of documents to be bulk inserted. What op did with
curl -X GET http://127.0.0.1:5984/cozy/_all_docs?include_docs=true > cozy.dump
was that he simply stored the couchdb response of the format
{
total_rows: 4,
offset: 0,
rows: [....]
}
into the cozy.dump file. As we have seen above this file is not in a form that can be consumed by the bulk docs api. Hence the error
{"error":"bad_request","reason":"Missing JSON list of 'docs'"}
Couchdb needs a JSON list of docs to perform the bulk insert.
Another point to be noted here is that if you supply an _id and _rev parameter couchdb performs a bulk update rather than a bulk insert. If you just want to copy one database to another use http://wiki.apache.org/couchdb/Replication