Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
af6ef157dc | ||
|
|
a5bcd663ee |
@@ -0,0 +1,305 @@
|
||||
# Github REST API
|
||||
|
||||
## GET issues
|
||||
|
||||
```bash
|
||||
$ curl -X GET https://api.github.com/repos/datamove/practice-repo/issues
|
||||
[
|
||||
{
|
||||
"url": "https://api.github.com/repos/datamove/practice-repo/issues/57",
|
||||
"repository_url": "https://api.github.com/repos/datamove/practice-repo",
|
||||
"labels_url": "https://api.github.com/repos/datamove/practice-repo/issues/57/labels{/name}",
|
||||
"comments_url": "https://api.github.com/repos/datamove/practice-repo/issues/57/comments",
|
||||
"events_url": "https://api.github.com/repos/datamove/practice-repo/issues/57/events",
|
||||
"html_url": "https://github.com/datamove/practice-repo/pull/57",
|
||||
"id": 727358338,
|
||||
"node_id": "MDExOlB1bGxSZXF1ZXN0NTA4MjY1NDgy",
|
||||
"number": 57,
|
||||
"title": "add ekatkanovitka.sh",
|
||||
"user": {
|
||||
"login": "ekatkanovitka",
|
||||
"id": 60265043,
|
||||
"node_id": "MDQ6VXNlcjYwMjY1MDQz",
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/60265043?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/ekatkanovitka",
|
||||
"html_url": "https://github.com/ekatkanovitka",
|
||||
"followers_url": "https://api.github.com/users/ekatkanovitka/followers",
|
||||
"following_url": "https://api.github.com/users/ekatkanovitka/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/ekatkanovitka/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/ekatkanovitka/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/ekatkanovitka/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/ekatkanovitka/orgs",
|
||||
"repos_url": "https://api.github.com/users/ekatkanovitka/repos",
|
||||
"events_url": "https://api.github.com/users/ekatkanovitka/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/ekatkanovitka/received_events",
|
||||
"type": "User",
|
||||
"site_admin": false
|
||||
},
|
||||
"labels": [
|
||||
|
||||
],
|
||||
"state": "open",
|
||||
"locked": false,
|
||||
"assignee": null,
|
||||
"assignees": [
|
||||
|
||||
],
|
||||
"milestone": null,
|
||||
"comments": 0,
|
||||
"created_at": "2020-10-22T12:48:06Z",
|
||||
"updated_at": "2020-10-22T12:48:06Z",
|
||||
"closed_at": null,
|
||||
"author_association": "NONE",
|
||||
"active_lock_reason": null,
|
||||
"pull_request": {
|
||||
"url": "https://api.github.com/repos/datamove/practice-repo/pulls/57",
|
||||
"html_url": "https://github.com/datamove/practice-repo/pull/57",
|
||||
"diff_url": "https://github.com/datamove/practice-repo/pull/57.diff",
|
||||
"patch_url": "https://github.com/datamove/practice-repo/pull/57.patch"
|
||||
},
|
||||
"body": "",
|
||||
"performed_via_github_app": null
|
||||
}
|
||||
]
|
||||
```
|
||||
|
||||
Заметьте, что вернулся массив. По умолчанию, выдаются только открытые тикеты и пулл-реквесты. Можно передать параметер для выбора статуса тикета
|
||||
|
||||
```bash
|
||||
$ curl -X GET https://api.github.com/repos/datamove/practice-repo/issues?state=closed
|
||||
```
|
||||
|
||||
Отфильтруем записи с помощью jq:
|
||||
|
||||
```bash
|
||||
$ curl https://api.github.com/repos/datamove/practice-repo/issues?state=closed | jq '.[].title' % Total % Received % Xferd Average Speed Time Time Time Current
|
||||
Dload Upload Total Spent Left Speed
|
||||
100 75050 100 75050 0 0 295k 0 --:--:-- --:--:-- --:--:-- 295k
|
||||
"added kadetfrolov.py"
|
||||
"added aliya_khabirzyanova.py"
|
||||
"added file oleg_shulgin.py"
|
||||
"added dmitry_kalinin.py"
|
||||
"added hethwynq.py"
|
||||
...
|
||||
```
|
||||
|
||||
Самое время убрать progress-bar. Используйте -s.
|
||||
|
||||
|
||||
#### Спецификация https://developer.github.com/v3/issues/#list-repository-issues
|
||||
|
||||
## POST an issue
|
||||
|
||||
reference - https://developer.github.com/v3/issues/#create-an-issue
|
||||
|
||||
```bash
|
||||
$ curl -X POST https://api.github.com/repos/datamove/practice-repo/issues
|
||||
{
|
||||
"message": "Not Found",
|
||||
"documentation_url": "https://docs.github.com/rest/reference/issues#create-an-issue"
|
||||
}
|
||||
```
|
||||
|
||||
Как же так? На самом деле, мы с этим уже встречались - требуется аутентификация.
|
||||
|
||||
### Get a token
|
||||
|
||||
Залогиньтесь на сайт github.com, в верхнем углу около вашей иконки разверните меню и выберите Settings (это глобальные настройки). Далее выберите Developer settings, Personal access tokens. Нажмите Generate new token.
|
||||
|
||||

|
||||
|
||||
Скопируйте сгенерированный токен в буфер и вставьте в файл public_repo_token под вашим аккаунтом на сервере и сохраните файл.
|
||||
|
||||
Тут же ограничьте доступ к этому файлу:
|
||||
|
||||
`chmod 600 public_repo_token`
|
||||
|
||||
### token в заголовке
|
||||
```
|
||||
$ H2="Authorization: token "`cat public_repo_token`
|
||||
$ echo $H2
|
||||
Authorization: token xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
|
||||
```
|
||||
(вместо иксов вы должны увидеть ваш токен)
|
||||
|
||||
### Используем заголовок в запросе
|
||||
|
||||
```bash
|
||||
$ curl -X POST -H "$H2" https://api.github.com/repos/datamove/practice-repo/issues
|
||||
{
|
||||
"message": "Invalid request.\n\nFor 'links/0/schema', nil is not an object.",
|
||||
"documentation_url": "https://docs.github.com/rest/reference/issues#create-an-issue"
|
||||
}
|
||||
```
|
||||
|
||||
Тут тоже все понятно, мы ведь никаких данных для тикета не отправили. Добавляем данные - объект json с полями title, body.
|
||||
|
||||
```bash
|
||||
$ curl -X POST -H "$H2" https://api.github.com/repos/datamove/practice-repo/issues -d '{"title":"test issue from datamove", "body": "yep, just a test!"}'
|
||||
{
|
||||
"url": "https://api.github.com/repos/datamove/practice-repo/issues/63",
|
||||
"repository_url": "https://api.github.com/repos/datamove/practice-repo",
|
||||
"labels_url": "https://api.github.com/repos/datamove/practice-repo/issues/63/labels{/name}",
|
||||
"comments_url": "https://api.github.com/repos/datamove/practice-repo/issues/63/comments",
|
||||
"events_url": "https://api.github.com/repos/datamove/practice-repo/issues/63/events",
|
||||
"html_url": "https://github.com/datamove/practice-repo/issues/63",
|
||||
"id": 734609678,
|
||||
"node_id": "MDU6SXNzdWU3MzQ2MDk2Nzg=",
|
||||
"number": 63,
|
||||
"title": "test issue from datamove",
|
||||
"user": {
|
||||
"login": "artemtrunov",
|
||||
"id": 69344942,
|
||||
"node_id": "MDQ6VXNlcjY5MzQ0OTQy",
|
||||
"avatar_url": "https://avatars1.githubusercontent.com/u/69344942?v=4",
|
||||
"gravatar_id": "",
|
||||
"url": "https://api.github.com/users/artemtrunov",
|
||||
"html_url": "https://github.com/artemtrunov",
|
||||
"followers_url": "https://api.github.com/users/artemtrunov/followers",
|
||||
"following_url": "https://api.github.com/users/artemtrunov/following{/other_user}",
|
||||
"gists_url": "https://api.github.com/users/artemtrunov/gists{/gist_id}",
|
||||
"starred_url": "https://api.github.com/users/artemtrunov/starred{/owner}{/repo}",
|
||||
"subscriptions_url": "https://api.github.com/users/artemtrunov/subscriptions",
|
||||
"organizations_url": "https://api.github.com/users/artemtrunov/orgs",
|
||||
"repos_url": "https://api.github.com/users/artemtrunov/repos",
|
||||
"events_url": "https://api.github.com/users/artemtrunov/events{/privacy}",
|
||||
"received_events_url": "https://api.github.com/users/artemtrunov/received_events",
|
||||
"type": "User",
|
||||
"site_admin": false
|
||||
},
|
||||
"labels": [
|
||||
|
||||
],
|
||||
"state": "open",
|
||||
"locked": false,
|
||||
"assignee": null,
|
||||
"assignees": [
|
||||
|
||||
],
|
||||
"milestone": null,
|
||||
"comments": 0,
|
||||
"created_at": "2020-11-02T15:42:17Z",
|
||||
"updated_at": "2020-11-02T15:42:17Z",
|
||||
"closed_at": null,
|
||||
"author_association": "CONTRIBUTOR",
|
||||
"active_lock_reason": null,
|
||||
"body": "yep, just a test!",
|
||||
"closed_by": null,
|
||||
"performed_via_github_app": null
|
||||
}
|
||||
```
|
||||
|
||||
Теперь можно снова посмотреть тикеты и найти там вновь созданный. Сразу отфильтруем собственные тикеты (подставьте ваш ник вместо artemtrunov):
|
||||
|
||||
`$ curl -X GET https://api.github.com/repos/datamove/practice-repo/issues?creator=artemtrunov`
|
||||
|
||||
## Изменяем тикет
|
||||
|
||||
Reference- https://developer.github.com/v3/issues/#update-an-issue
|
||||
|
||||
```bash
|
||||
$ curl -X PATCH -H "$H2" https://api.github.com/repos/datamove/practice-repo/issues/63 -d '{"title":"Patched test issue from datamove", "body": "yep, just a test!"}'
|
||||
{
|
||||
"url": "https://api.github.com/repos/datamove/practice-repo/issues/63",
|
||||
"repository_url": "https://api.github.com/repos/datamove/practice-repo",
|
||||
"labels_url": "https://api.github.com/repos/datamove/practice-repo/issues/63/labels{/name}",
|
||||
"comments_url": "https://api.github.com/repos/datamove/practice-repo/issues/63/comments",
|
||||
"events_url": "https://api.github.com/repos/datamove/practice-repo/issues/63/events",
|
||||
"html_url": "https://github.com/datamove/practice-repo/issues/63",
|
||||
"id": 734609678,
|
||||
"node_id": "MDU6SXNzdWU3MzQ2MDk2Nzg=",
|
||||
"number": 63,
|
||||
"title": "Patched test issue from datamove",
|
||||
"user": {
|
||||
"login": "artemtrunov",
|
||||
...
|
||||
```
|
||||
|
||||
## Удаляем
|
||||
|
||||
```bash
|
||||
$ curl -X DELETE -H "$H2" https://api.github.com/repos/datamove/practice-repo/issues/63
|
||||
{
|
||||
"message": "Not Found",
|
||||
"documentation_url": "https://docs.github.com/rest"
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
Опять!! Тут дело в том, что гитхаб не дает возможность удалять через API.
|
||||
Но можно изменить статус на `Closed` запросом PATCH.
|
||||
|
||||
### Используем curl в скриптах
|
||||
|
||||
* -i выводить заголовок HTTP-ответа и само сообщение (payload)
|
||||
* -I выводить только заголовок HTTP-ответа
|
||||
* -s - не печатать progress bar
|
||||
* -f (--fail) - выходить с ненулевым кодом
|
||||
* -S выводить ошибки при ошибках
|
||||
* -sw '%{http_code}' - выводить только статус-код запроса
|
||||
|
||||
## Как же все таки удадить issue? GraphQL
|
||||
|
||||
Это пока еще экспериментальное api v4. Довольно спорное.
|
||||
|
||||
Нам понадобится не просто номер тикета 63, а идентификатор узла этого тикета, видимо где-то в глобальном графе ресурсов. В принципе, он есть и в выводе результата вызова API v3, который мы делали до этого: `"node_id": "MDU6SXNzdWU3MzQ2MDk2Nzg=",`, но мы попробуем и аналог GET запроса на GraphQL.
|
||||
|
||||
Сразу обратите внимание, что даже запрос на чтение в GraphQL - это POST запрос!!
|
||||
|
||||
```bash
|
||||
curl -X POST -H "$H2" https://api.github.com/graphql -d '{"query": "query FindIssueID {repository(owner: \"datamove\", name: \"practice-repo\") {issue(number:63){id}}}"}'
|
||||
{"data":{"repository":{"issue":{"id":"MDU6SXNzdWU3MzQ2MDk2Nzg="}}}}
|
||||
```
|
||||
|
||||
```
|
||||
query FindIssueID {
|
||||
repository(owner: \"datamove\", name: \"practice-repo\") {
|
||||
issue(number:63){id}
|
||||
}
|
||||
}
|
||||
```
|
||||
FindIssueID - просто идентификатор запроса, может быть любым
|
||||
|
||||
Удаляем с помощью mutation.
|
||||
|
||||
```bash
|
||||
$ curl -X POST -H "$H2" https://api.github.com/graphql -d '{"query": "mutation { deleteIssue(input:{issueId:\"MDU6SXNzdWU3MzQ2MDk2Nzg=\"}) { repository {name}} }" }'
|
||||
{"data":{"deleteIssue":null},"errors":[{"type":"FORBIDDEN","path":["deleteIssue"],"locations":[{"line":1,"column":12}],"message":"artemtrunov does not have the correct permissions to execute `DeleteIssue`"}]}
|
||||
```
|
||||
```
|
||||
mutation {
|
||||
deleteIssue(input:{issueId:\"MDU6SXNzdWU3MzQ2MDk2Nzg=\"}) {
|
||||
repository {name}
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Из документации:
|
||||
|
||||
_Deleting an issue_
|
||||
|
||||
_People with admin permissions in a repository can permanently delete an issue from a repository.
|
||||
Did this doc help you?_
|
||||
|
||||
_You can only delete issues in a repository owned by your user account. You cannot delete issues in a repository owned by another user account, even if you are a collaborator there._
|
||||
|
||||
Как и обещала документация, только хозяин репо может удалить тикеты. С токеном хозяина репо:
|
||||
|
||||
```bash
|
||||
$ curl -X POST -H "$H2" https://api.github.com/graphql -d '{"query": "mutation { deleteIssue(input:{issueId:\"MDU6SXNzdWU3MzQ2MDk2Nzg=\"}) { repository {name}} }" }'
|
||||
{"data":{"deleteIssue":{"repository":{"name":"practice-repo"}}}}
|
||||
```
|
||||
|
||||
Удалил? Проверяем:
|
||||
|
||||
```bash
|
||||
artem@artem-ubuntu2:/media/data/ozon$ curl -X POST -H "$H2" https://api.github.com/graphql -d '{"query": "query FindIssueID {repository(owner: \"datamove\", name: \"practice-repo\") {issue(number:63){id}}}"}'
|
||||
{"data":{"repository":{"issue":null}},"errors":[{"type":"NOT_FOUND","path":["repository","issue"],"locations":[{"line":1,"column":74}],"message":"Could not resolve to an Issue with the number of 63."}]}
|
||||
```
|
||||
### Ссылки на доки по GraphQL
|
||||
|
||||
* https://docs.github.com/en/free-pro-team@latest/graphql/reference/mutations#deleteissue
|
||||
* https://developer.github.com/v4/guides/forming-calls/#example-mutation
|
||||
|
||||
## Удалите токен через тут же страницу девелоперских настроек, от греха подальше!
|
||||
+299
@@ -0,0 +1,299 @@
|
||||
# jq
|
||||
|
||||
В этом примере используется датасет `/home/datamove/meta_Luxury_Beauty.json`
|
||||
|
||||
## jq - форматирование
|
||||
|
||||
Без опций и аргументов используется для форматирования json.
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq
|
||||
{
|
||||
"category": [],
|
||||
"tech1": "",
|
||||
"description": [
|
||||
"After a long day of handling thorny situations, our new hand therapy pump is just the help you need. It contains shea butter as well as extracts of yarrow, clover and calendula to help soothe and condition work-roughened hands.",
|
||||
...
|
||||
```
|
||||
|
||||
## jq - фильтр
|
||||
|
||||
### .
|
||||
|
||||
Точка - это текущая запись, или вернее, указатель на ее начало (корень)
|
||||
|
||||
### Взять индивидуальное поле
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.title'
|
||||
"Crabtree & Evelyn - Gardener's Ultra-Moisturising Hand Therapy Pump - 250g/8.8 OZ"
|
||||
"AHAVA Bath Salts"
|
||||
```
|
||||
|
||||
Так же точно выбираются и объекты (словари)
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.details'
|
||||
{
|
||||
"\n Product Dimensions: \n ": "2.2 x 2.2 x 7 inches ; 8.8 ounces",
|
||||
"Shipping Weight:": "14.4 ounces (",
|
||||
"Domestic Shipping: ": "Item can be shipped within U.S.",
|
||||
"International Shipping: ": "This item can be shipped to select countries outside of the U.S.",
|
||||
"ASIN:": "B00004U9V2",
|
||||
"Item model number:": "4113"
|
||||
}
|
||||
{
|
||||
"\n Product Dimensions: \n ": "3 x 3.5 x 6 inches ; 2.2 pounds",
|
||||
"Shipping Weight:": "2.6 pounds",
|
||||
"Domestic Shipping: ": "Item can be shipped within U.S.",
|
||||
"International Shipping: ": "This item is not eligible for international shipping.",
|
||||
"ASIN:": "B0000531EN",
|
||||
"Item model number:": "017N"
|
||||
}
|
||||
```
|
||||
|
||||
или поля объектов:
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.details."Shipping Weight:"'
|
||||
"14.4 ounces ("
|
||||
"2.6 pounds"
|
||||
```
|
||||
|
||||
заметьте кавычки для строк с пробелами и другими символами (например, двоеточие)
|
||||
|
||||
### Сделать новые json-записи
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '{title, ship_weight:.details."Shipping Weight:" }'
|
||||
{
|
||||
"title": "Crabtree & Evelyn - Gardener's Ultra-Moisturising Hand Therapy Pump - 250g/8.8 OZ",
|
||||
"ship_weight": "14.4 ounces ("
|
||||
}
|
||||
{
|
||||
"title": "AHAVA Bath Salts",
|
||||
"ship_weight": "2.6 pounds"
|
||||
}
|
||||
```
|
||||
|
||||
Заметьте, что если не меняем название поля, то можно сократить `title:.title` до `title`.
|
||||
|
||||
### Сделать новые json-записи из комбинации полей
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '{title:(.main_cat + " - " + .title), ship_weight:.details."Shipping Weight:" }'
|
||||
{
|
||||
"title": "Luxury Beauty - Crabtree & Evelyn - Gardener's Ultra-Moisturising Hand Therapy Pump - 250g/8.8 OZ",
|
||||
"ship_weight": "14.4 ounces ("
|
||||
}
|
||||
{
|
||||
"title": "Luxury Beauty - AHAVA Bath Salts",
|
||||
"ship_weight": "2.6 pounds"
|
||||
}
|
||||
```
|
||||
|
||||
## последовательность фильтров
|
||||
|
||||
Как и в баше, можно сделать конвеер с помощью |
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.details | ."Shipping Weight:"'
|
||||
"14.4 ounces ("
|
||||
"2.6 pounds"
|
||||
```
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.details | "ASIN", ."Shipping Weight:" '
|
||||
"ASIN"
|
||||
"14.4 ounces ("
|
||||
"ASIN"
|
||||
"2.6 pounds"
|
||||
```
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.details | { ASIN:."ASIN:", ship_weight:."Shipping Weight:" } '
|
||||
{
|
||||
"ASIN": "B00004U9V2",
|
||||
"ship_weight": "14.4 ounces ("
|
||||
}
|
||||
{
|
||||
"ASIN": "B0000531EN",
|
||||
"ship_weight": "2.6 pounds"
|
||||
}
|
||||
```
|
||||
|
||||
Работает так же "внутри" нового словаря:
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '{asin, ship_weight:.details."Shipping Weight:" | sub("pounds"; "p.") | sub("ounces"; "ou.") }'
|
||||
{
|
||||
"asin": "B00004U9V2",
|
||||
"ship_weight": "14.4 ou. ("
|
||||
}
|
||||
{
|
||||
"asin": "B0000531EN",
|
||||
"ship_weight": "2.6 p."
|
||||
}
|
||||
```
|
||||
|
||||
Разделитель _запятая_ имеет приоритет перед разделителем _палка_. Но можно (и даже полезно для наглядности) использовать группировку:
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '{asin, ship_weight:(.details."Shipping Weight:" | sub("pounds"; "p.") | sub("ounces"; "ou.")) }'
|
||||
```
|
||||
|
||||
### Работа с массивами
|
||||
|
||||
Обращение к массиву осуществляется посредством скобок []
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.also_buy[0]'
|
||||
"B00GHX7H0A"
|
||||
null
|
||||
```
|
||||
|
||||
ИЛИ
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.also_buy | .[0]'
|
||||
"B00GHX7H0A"
|
||||
null
|
||||
|
||||
```
|
||||
|
||||
Cрез массива: `.[2:4]`
|
||||
|
||||
`massive[].field` - все поля field элементов массива
|
||||
|
||||
длина массива
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.also_buy|length'
|
||||
32
|
||||
0
|
||||
```
|
||||
|
||||
Сделать что угодно с елементами массива:
|
||||
|
||||
```bash
|
||||
datamove@linux1:~$ cat meta_Luxury_Beauty.json | head -2 | jq '.also_buy | map("asin_" + .)'
|
||||
[
|
||||
"asin_B00GHX7H0A",
|
||||
"asin_B00FRERO7G",
|
||||
"asin_B00R68QXCS",
|
||||
...
|
||||
```
|
||||
|
||||
|
||||
### Модификация полей
|
||||
|
||||
Очень часто надо просто заменить значения наскольких полей. Достигается знаком присвоения =.
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.details."Shipping Weight:"=(.details."Shipping Weight:" | sub("pounds"; "p.") | sub("ounces"; "ou."))'
|
||||
```
|
||||
|
||||
Или завести новые поля:
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.rating=4.5' | tail -5
|
||||
"date": "",
|
||||
"price": "",
|
||||
"asin": "B0000531EN",
|
||||
"rating": 4.5
|
||||
}
|
||||
```
|
||||
|
||||
Или удалить какие-то:
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.rating=4.5 | del(.date)' | tail -5
|
||||
"similar_item": "",
|
||||
"price": "",
|
||||
"asin": "B0000531EN",
|
||||
"rating": 4.5
|
||||
}
|
||||
```
|
||||
|
||||
также работает и объединенное присваивание: `|= += -= *= /= %= //=`
|
||||
|
||||
##
|
||||
|
||||
## Логические выражения
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq '.asin=="B0000531EN"'
|
||||
false
|
||||
true
|
||||
```
|
||||
|
||||
#### Выбор записей на основе логического выражения
|
||||
|
||||
```bash
|
||||
$ cat meta_Luxury_Beauty.json | head -2 | jq 'select(.asin=="B0000531EN") | .asin'
|
||||
"B0000531EN"
|
||||
```
|
||||
|
||||
Но это не всегда так просто, иногда нужна промежуточная переменная:
|
||||
|
||||
`([ .[].number ] | min) as $m | map(select(.number== $m))`
|
||||
|
||||
Впрочем, выбирать элемент массива по min,max какого-то поля можно и с помощью встроенной функции:
|
||||
|
||||
`max_by(.asin)`
|
||||
|
||||
|
||||
### Функции
|
||||
|
||||
* со строками
|
||||
|
||||
startswith("foo") endswith("foo") length ascii_upcase ascii_downcase sub("x", "y") gsub("x", "y")
|
||||
|
||||
tonumber->number split->array index("string")->number
|
||||
|
||||
test - регулярное выражение
|
||||
capture - вернуть сматченные значения
|
||||
|
||||
* с числами
|
||||
|
||||
exp log sin cos tan asin acos atan floor sqrt
|
||||
|
||||
tostring->string
|
||||
|
||||
* с массивами
|
||||
|
||||
sort reverse unique contains inside
|
||||
|
||||
length,map,group,reduce, min, max
|
||||
|
||||
`echo '[1,2,3,4]'| jq 'reduce .[] as $item (0; .+$item)'`
|
||||
|
||||
all, any, flaten
|
||||
|
||||
join->string
|
||||
|
||||
* с датами
|
||||
|
||||
now gmtime todate mktime
|
||||
|
||||
string->strptime
|
||||
|
||||
strftime->string
|
||||
|
||||
|
||||
## А так же
|
||||
|
||||
* циклы
|
||||
* if then else
|
||||
* обработка исключений
|
||||
* определение функций
|
||||
* определение переменных
|
||||
* использование функций из библиотек
|
||||
* определение типа данных
|
||||
* экспорт в другие форматы (csv, base64, URL quoting ...)
|
||||
|
||||
## Ссылки
|
||||
|
||||
* https://hyperpolyglot.org/json - компактный справочник
|
||||
* https://stedolan.github.io/jq/manual/ - полное руководство
|
||||
|
||||
Reference in New Issue
Block a user