How to pick up a value from a JSON in bash? - json

I tried many things to pick up all the values in relatives from JSON but didn't find how to make it works.
Thanks for any help and/or any advice.
I need to do it with the export ID=1 at first, I can only edit the second line with curl and jq.
bash:
export ID=1
curl 'url.json' | jq -r '.ID.relatives'
JSON:
"biography": {
"fullName": "Richard Milhouse Jones",
],
"placeOfBirth": "Scarsdale, Arizona",
"firstAppearance": "Hulk Vol 2 #2 (April, 2008) (as A-Bomb)",
"publisher": "Marvel Comics",
},
"connections": {
"groupAffiliation": "Hulk Family; Excelsior (sponsor), Avengers (honorary member)",
"relatives": "Marlo Chandler-Jones (wife); Polly (aunt); Mrs. Chandler (mother-in-law); Keith Chandler, Ray Chandler, three unidentified others (brothers-in-law)"

Related

Search and extract value using JQ command line processor

I have a JSON file very similar to the following:
[
{
"uuid": "832390ed-58ed-4338-bf97-eb42f123d9f3",
"name": "Nacho"
},
{
"uuid": "5b55ea5e-96f4-48d3-a258-75e152d8236a",
"name": "Taco"
},
{
"uuid": "a68f5249-828c-4265-9317-fc902b0d65b9",
"name": "Burrito"
}
]
I am trying to figure out how to use the JQ command line processor to first find the UUID that I input and based on that output the name of the associated item. So for example, if I input UUID a68f5249-828c-4265-9317-fc902b0d65b9 it should search the JSON file, find the matching UUID and then return the name Burrito. I am doing this in Bash. I realize it may require some outside logic in addition to JQ. I will keep thinking about it and put an update here in a bit. I know I could do it in an overly complicated way, but I know there is probably a really simple JQ method of doing this in one or two lines. Please help me.
https://shapeshed.com/jq-json/#how-to-find-a-key-and-value
You can use select:
jq -r --arg query Burrito '.[] | select( .name == $query ) | .uuid ' tst.json

Using jq to parse json key and value to CSV

I am a newbie to jq and is very excited to use it. What ever i am trying to achieve is possible with python but the intention is to learn jq.I am trying to process JSON out of a curl command.
Below is the response of my curl command
{
"results": [{
"name": "smith Jones",
"DOB": "1992-03-26",
"Enrollmentdate": "2013-08-24"
},
{
"name": "Jacob Mathew",
"DOB": "1993-03-26",
"Enrollmentdate": "2014-10-02"
},
{
"name": "Anita Rodrigues",
"DOB": "1994-03-26",
"Enrollmentdate": "2015-02-19"
}
]
}
I was able to get the desired output to some extent. But i am unable to print the key itself in the output. I need this information to use it at a later time as a header of the column when i export this csv file (file.csv) into excel. I am planning to write a bash script to achieve the csv to excel.
<curl-command>|jq '.results | map(.name), map(.DOB), map(.Enrollmentdate) | #csv' >file.csv
I was able to get the output as below
smith jones, jacob Mathew, Anita Rodrigues
1992-03-26, 1993-03-26, 1994-03-26
2013-08-24, 2014-10-02, 2015-02-19
What i am trying to achieve is as below
name:smith jones, name:jacob Mathew, name:Anita Rodrigues
DOB:1992-03-26, DOB:1993-03-26, DOB:1994-03-26
Enrollmentdate:2013-08-24, Enrollmentdate:2014-10-02, Enrollmentdate:2015-02-19
Since you want the key names as well as their values, then adapting your approach, you could use the following, in conjunction with the -r command-line option, to produce CSV:
.results
| map(to_entries[] | select(.key=="name")),
map(to_entries[] | select(.key=="DOB")),
map(to_entries[] | select(.key=="Enrollmentdate"))
| map("\(.key):\(.value)" )
| #csv`
If you want CSV, then stick with the above; if you are confident that quoting the strings
is never necessary, change #csv to join(", "); if you want to remove the quotation
marks only when they are not necessary, you could add a def for a simple filter to do just that.
The repetition of to_entries in the above is a bit of an eye-sore. You might want to think about how to avoid it.

Paypal Subscription Setup

I am trying to setup my three subscriptions using curl and im making sure the values are correct. Trying to do a request and get a response but my curl is not functioning corectly.
https://developer.paypal.com/docs/api/subscriptions/v1/#plans_create
My intention:
3 Plans to choose from: Elite (149 PHP/MONTH), Premium (349 PHP/MONTH), Luxury (549 PHP/MONTH)
1 month is 30 days as paypal said
id like to set it to auto renew monthly until the customer chooses to cancel it.
only one will be active at a time, if a user chooses another of the three while one is running, they will automatically be stopped getting billed and will be charged with the new one chosen. (eg: currently elite149, the subscription and renewal will change to 549luxury once chosen).
curl -v -X POST https://api.sandbox.paypal.com/v1/billing/plans \
-H "Content-Type: application/json" \
-H "Authorization: Basic account_clientid:account_secretcode" \
-H "PayPal-Request-Id: EPL-25012019-001" \
-d '{
"product_id": "MWC-2019EPL",
"name": "My White Card Subscription Plans",
"description": "MyWhiteCard Membership Levels",
"status": "ACTIVE",
"billing_cycles": [
{
"frequency": {
"interval_unit": "MONTH",
"interval_count": 1
},
"tenure_type": "REGULAR",
"sequence": 1,
"total_cycles": 999,
"pricing_scheme": {
"fixed_price": {
"value": "149",
"currency_code": "PHP"
}
}
},
{
"frequency": {
"interval_unit": "MONTH",
"interval_count": 1
},
"tenure_type": "REGULAR",
"sequence": 2,
"total_cycles": 999,
"pricing_scheme": {
"fixed_price": {
"value": "349",
"currency_code": "PHP"
}
}
},
{
"frequency": {
"interval_unit": "MONTH",
"interval_count": 1
},
"tenure_type": "REGULAR",
"sequence": 3,
"total_cycles": 999,
"pricing_scheme": {
"fixed_price": {
"value": "549",
"currency_code": "PHP"
}
}
}
],
"payment_preferences": {
"auto_bill_outstanding": true,
"setup_fee": {
"value": "0",
"currency_code": "PHP"
},
"setup_fee_failure_action": "CONTINUE",
"payment_failure_threshold": 3
},
"taxes": {
"percentage": "10",
"inclusive": false
}
}'
I just took out my account's client_id:secret
Live credentials are being used
My questions and concerns:
In the access token, do I need to put the "access_token$production$" and then the code given?
Can I manually create the Paypal request id and the product id?
Will the billing cycles be all on the same command or do I have to trigger this three times?
Is the setup fee the charge if a customer subscribes? I first assumed that is the case and set it to 0.
My intention is to have the monthly subscription (30 Days) auto renew until the user unsubscribes. I set my example to 12 but will "total_cycles": NEVER be the correct input?
I am not sure how the tax part works, why am I the one that gets to modify it?
Additional Concerns:
The document does not show the live equivalent of the link https://api.sandbox.paypal.com/v1/billing/plans is it just simply https://api.live.paypal.com/v1/billing/plans?
I tried to add the above code using git bash and curl but as I run it it shows 1008{"error":"invalid_client","error_description":"Client credentials are missing"}
Do I try to run the curl code in Git bash and not change my directory? I just start git and run the curl here:
Any help will be appreciated. I just making sure everything is what needs to be because these three subscriptions will go to a live website. I have to be certain only one subscription runs at a time.
UPDATE: I tried making it to curl -v -X POST https://api.production.paypal.com/v1/billing/plans and this one is not functionning either.
UPDATE: Can anyone show an example of a working curl sample request?
UPDATE: at the -H "Authorization: Basic account_clientid:account_secretcode", just to be clear I took out my id code there since I cant just show it in public. An example that I placed there is
-H "Authorization: Basic JAKRc85nJy2eMLq3aIV:01PvLC934xMAwLHqU4JqA89as4N"
UPDATE I tried to run this curl in git after reading the answers so far and somehow I still get and error. I made sure that the api is in api.paypal.com and the client id and secret id is the live version.
curl -v -X POST https://api.paypal.com/v1/billing/plans \
-H "Content-Type: application/json" \
-H "Authorization: Basic AR7nnwwotKOt4YdcGHZc0P2RVsRT67_Gf2hyrKyDl3ZgCKsikeKbXdQ9Fj-_21v4RulkXsgAASe7_VKv:EKwsdDo1ehtOOOSZCGMu1C9903qr4cQOOZI2rgFYhvugh2SO1V04q9MWY9SXwa352zBt1mGglLuWgR4D" \
-H "PayPal-Request-Id: MWC-2501E-001" \
-d '{
"product_id": "ELITE-2501149",
"name": "Elite Membership",
"description": "Elite Membership Monthly Plan",
"status": "ACTIVE",
"billing_cycles": [
{
"frequency": {
"interval_unit": "MONTH",
"interval_count": 1
},
"tenure_type": "REGULAR",
"sequence": 1,
"total_cycles": 999,
"pricing_scheme": {
"fixed_price": {
"value": "149",
"currency_code": "PHP"
}
}
}
],
"payment_preferences": {
"auto_bill_outstanding": true,
"setup_fee": {
"value": "0",
"currency_code": "PHP"
},
"setup_fee_failure_action": "CONTINUE",
"payment_failure_threshold": 3
},
"taxes": {
"percentage": "12",
"inclusive": false
}
}'
I get this error:
100 921 100 159 100 762 99 478 0:00:01 0:00:01 --:--:-- 578{"name":"INTERNAL_SERVICE_ERROR","debug_id":"1975a4fe9232","links":[{"href":"https://developer.paypal.com/docs/api/overview/#error","rel":"information_link"}]}
UPDATE
Read all the links and followed the steps, I think this is the only issue left
{"name":"NOT_AUTHORIZED","message":"Authorization failed due to insufficient permissions.","debug_id":"484a9d7460069","details":[{"issue":"PERMISSION_DENIED","description":"You do not have permission to access or perform operations on this resource"}],"links":[{"href":"https://developer.paypal.com/docs/api/v1/billing/subscriptions#NOT_AUTHORIZED","rel":"information_link","method":"GET"}]}
I checked the Paypal help center and it seems that I need someone from Paypal itself to authorize my REST.
Q1 - In the access token, do I need to put the "access_token$production$" and then the code given?
In your example, you are using Basic authentication scheme. You can use:
-H "Authorization: Bearer Access-Token" \
But to answer your question, no you don't need to specifically include the access_token text string.
Q2 - Can I manually create the Paypal request id and the product id?
Yes, request id is used so you can retry your API calls.
https://developer.paypal.com/docs/api/reference/api-requests/#http-request-headers
HOWEVER product_id should come from https://developer.paypal.com/docs/api/catalog-products/v1/#products_create
so you will need to do it first. See my update at the bottom part of this answer.
Q3 - Will the billing cycles be all on the same command or do I have to trigger this three times?
You will need to call each plan configuration to create three subscription plans. Basically, in the API doc example, it shows you it has a billing cycle created for a Trial period and the regular plan it self. It also says:
https://developer.paypal.com/docs/api/subscriptions/v1/#plans_create
An array of billing cycles for trial and regular billing. A plan can
have multiple billing cycles but only one regular billing cycle.
You also mentioned:
only one will be active at a time, if a user chooses another of the
three while one is running, they will automatically be stopped getting
billed and will be charged with the new one chosen. (eg: currently
elite149, the subscription and renewal will change to 549luxury once
chosen).
You'll need to do this programmatically on your end.
If a user unsubscribes to a plan, then you will need to cancel his subscription by calling:
https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_cancel
If you plan to change subscription, then first you need to cancel existing, then subscribe the user to the new subscription plan using: https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create
Q4 - Is the setup fee the charge if a customer subscribes? I first assumed that is the case and set it to 0.
The setup fee is just an add-on fee you can charge your customers. You will need to manually declare this or make it optional by making setup fee value 0.
https://developer.paypal.com/docs/api/subscriptions/v1/#definition-payment_preferences
Q5 - My intention is to have the monthly subscription (30 Days) auto renew until the user unsubscribes. I set my example to 12 but will "total_cycles": NEVER be the correct input?
The maximum value is 999. It only accepts integer.
https://developer.paypal.com/docs/api/subscriptions/v1/#definition-billing_cycle
Q6 - I am not sure how the tax part works, why am I the one that gets to modify it?
Because tax is dependent on what region or country you are in. You are using PHP or Philippine Peso as currency so it could mean that you are in the Philippines. You will need to setup your tax percentage as 12% as that is what is used for taxing goods and services
Source: https://www.full-suite.com/blog/whats-difference-vat-percentage-tax/
Paypal tax object definition: https://developer.paypal.com/docs/api/subscriptions/v1/#definition-taxes
Q7 - The document does not show the live equivalent of the link https://api.sandbox.paypal.com/v1/billing/plans is it just simply https://api.live.paypal.com/v1/billing/plans?
You can find the addresses here:
https://developer.paypal.com/docs/api/overview/#get-an-access-token
It says:
Sandbox: https://api.sandbox.paypal.com
Live: https://api.paypal.com
Q8 - I tried to add the above code using git bash and curl but as I run it it shows 1008{"error":"invalid_client","error_description":"Client credentials are missing"}
It could mean either mean that you are passing invalid credentials. Make sure that you are sending them via the headers using
Bearer <Access-Token>
or
Basic <client_id>:<secret>
And verify that your string input represents the actual values.
Also, make sure you are using Sandbox credentials as it has a different set of credentials from the production or live
https://developer.paypal.com/docs/classic/lifecycle/sb-create-accounts/#create-a-sandbox-account
Q9 - Do I try to run the curl code in Git bash and not change my directory? I just start git and run the curl here:
Do not misinterpret Git Bash as the command line interface, it is simply a versioning tool for your projects. However the Git Package for windows has built-in components that should allow you to run CURL. Since Curl is installed in the Bin directory, you should be able to run it on any directory.
You can run the curl command using different tools ideally using a scripting or programming language like PHP, Phyton, Java or even Node which has better support for curl and should allow you to write and test your program easier in a neater way.
UPDATE
I've investigated on this further. I thought I'd share it with you because it seems you haven't read the whole API document yet.
You are creating Subscription plans however you'll need to Create the products first. (I have updated my answer to Question #2)
First - Create the Product
This will create the product_id you need to create your subscription plans.
https://developer.paypal.com/docs/api/catalog-products/v1/#products_create
To go back and check the product you created an get its product id, you can use this api call:
https://developer.paypal.com/docs/api/catalog-products/v1/#products_list
Then - Create The Subscription Plan
After you have created your products, you can then create multiple subscription plans for it.
https://developer.paypal.com/docs/api/subscriptions/v1/#plans_create
Moving forward, if you want to subscribe / unsubscribe users you'll need to programatically do it as per my answer to Question #3.
About your problem with invalid credentials
Try getting the access token first. To do that follow instructions here:
https://developer.paypal.com/docs/api/get-an-access-token-curl/
curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
-H "Accept: application/json" \
-H "Accept-Language: en_US" \
-u "client_id:secret" \
-d "grant_type=client_credentials"
Then get the token returned and use that to make your api calls.
It will return something like this:
{
"scope": "scope",
"access_token": "Access-Token",
"token_type": "Bearer",
"app_id": "APP-80W284485P519543T",
"expires_in": 31349,
"nonce": "nonce"
}
Here is a modified version of your code that I used to test, notice it uses Bearer token instead basic client_id:secret
curl -v -X POST https://api.sandbox.paypal.com/v1/billing/plans \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token-goeshere" \
-H "PayPal-Request-Id: MWC-helper" \
-d '{
"product_id": "PROD-ELITETEST", //<---- this product id should be taken from the real product id when you created your product through product_create api.
"name": "Elite Membership",
"description": "Elite Membership Monthly Plan",
"status": "ACTIVE",
"billing_cycles": [
{
"frequency": {
"interval_unit": "MONTH",
"interval_count": 1
},
"tenure_type": "REGULAR",
"sequence": 1,
"total_cycles": 999,
"pricing_scheme": {
"fixed_price": {
"value": "149",
"currency_code": "PHP"
}
}
}
],
"payment_preferences": {
"auto_bill_outstanding": true,
"setup_fee": {
"value": "0",
"currency_code": "PHP"
},
"setup_fee_failure_action": "CONTINUE",
"payment_failure_threshold": 3
},
"taxes": {
"percentage": "12",
"inclusive": false
}
}'
It worked for me!

Monitoring disk usage in CentOS using 'du' to output JSON

I want to write a line of code which will take the results of:
du -sh -c --time /00-httpdocs/*
and output it in JSON format. The goal is to get three pieces of information for each project file in a site: directory path, date last modified, and disk space usage in human readable format. This command will output that data in tab-delimited format with each entry on a new line in the terminal:
4.6G 2014-08-22 12:26 /00-httpdocs/00
1.1G 2014-08-22 13:32 /00-httpdocs/01
711M 2014-02-14 23:39 /00-httpdocs/02
The goal is to get it to export to a JSON file so it would need to be formatted something like this:
{"httpdocs": [
{
"size": "4.6G",
"modified": "2014-08-22 12:26",
"path": "/00-httpdocs/00-PREVIEW"}
{
"size": "1.1G",
"modified": "2014-08-22 13:32",
"path": "/00-httpdocs/8oclock"}
{
"size": "711M",
"modified": "2014-02-14 23:39",
"path": "/00-httpdocs/8oclock.new"}
]}
(I know that's not quite proper JSON, I just wrote it as an example. Apologies to the pedantic among us.)
I need size to return as an integer (so maybe remove '-sh' and handle conversion later?).
I've tried using awk and sed but I'm a total novice and can't quite get the formatting right.
I've made it about this far:
du -sh -c --time /00-httpdocs/* | awk ' BEGIN {print "\"httpdocs:\": [";} {print "{"$0"},\n";} END {print "]";}'
The goal is to have this trigger twice a day so that we can get the data and use it inside of a JavaScript application.
sed '1 i\
{"httpdocs": [
s/\([^[:space:]]*\)([[:space:]]*\([^[:space:]]*\)[[:space:]]*\([^[:space:]]*\)/ {\
"size" : "\1",\
"modified": "\2",\
"path": "\3"}/
$ a\^J]}' YourFile
Quick and dirty (posix version so --posix on GNU sed).
Take the 3 argument and place them (s/../../) into a 'template" using group (\( ...\) and \1).
Include header at 1st line (i \...) and append footer ant last (a \...).
[:space:] may be [:blank:]

How do i import JSON file consists more than 200 documents into mongodb

I have created a demodb.json file (having 10 documents) and kept it in my system's D:\ drive
contents of demodb.json are:
[{"ID": 1,"Name": "Kabul","CountryCode": "AFG","District": "Kabol","Population": 1780000}, {"ID": 2,"Name": "Qandahar","CountryCode": "AFG","District": "Qandahar","Population": 237500}, {"ID": 3,"Name": "Herat","CountryCode": "AFG","District": "Herat","Population": 186800}, {"ID": 4,"Name": "Mazar-e-Sharif","CountryCode": "AFG","District": "Balkh","Population": 127800}, {"ID": 5,"Name": "Amsterdam","CountryCode": "NLD","District": "Noord-Holland","Population": 731200}, {"ID": 6,"Name": "Rotterdam","CountryCode": "NLD","District": "Zuid-Holland","Population": 593321}, {"ID": 7,"Name": "Haag","CountryCode": "NLD","District": "Zuid-Holland","Population": 440900}, {"ID": 8,"Name": "Utrecht","CountryCode": "NLD","District": "Utrecht","Population": 234323}, {"ID": 9,"Name": "Eindhoven","CountryCode": "NLD","District": "Noord-Brabant","Population": 201843}, {"ID": 10,"Name": "Tilburg","CountryCode": "NLD","District": "Noord-Brabant","Population": 193238}]
My Mongodb location is: D:\SanMongoDB
All my database and collections stored in D:\SanMongoDB\MongoData
contents of demodb-->city collection is as below
{ "_id":ObjectId("51892b40842d69f5fa21962f"),
"Name": "India",
"CountryCode": "IND",
"District": "ODISHA",
"Population": 2050000
}
I started Mongod server then open mongo client
In mongo client I did the following:
use demodb
mongoimport -d demodb -c city --type json --file demodb.json --jsonarray
But i found error as below
Thu May 09 08:21:57.861 Javascript execution failed:SyntaxError:Unexpected identifier
Kindly suggest what I need to do so I can easily import the Json file into the exisiting database of Mongodb.
Assuming your stuff is in file.json, try sed -e 's/ID/_id/g;s/\([^:]*\):\([^,}]*\)/"\1":"\2"/g' file.json | mongoimport --collection collection-name. You can get sed for Windows from GNUWin32. Let me know if you have further problems.
Here, s/ID/_id/g substitutes every occurrences of ID with _id in the file file.json and the second quotes every word to make your file valid JSON. After the two transformations, mongoimport works fine.
Not 100 % sure but I think it should look like this :
Start mongod
use demodb
mongoimport -d [dbname] -c [collectionname] --demodb.json --jsonarray
some example
http://www.mkyong.com/mongodb/mongodb-import-and-export-example/