在咱们常常调试微服务或许运用 Elasticsearch API 时,常常会运用curl 来进行调试。但是有时咱们的输出不尽如意。显现的不是一 pretty 格局进行输出的。咱们有时还必须借助于其他的一些网站工具,比如Best JSON Formatter and JSON Validator: Online JSON Formatter 或许JSON Formatter & Validator来帮我来验证。
在 Elasticsearch 的输出中常常咱们会看到如下格局的指令:
curl -k -u elastic:gV4pgxNCTi5y*80GmoqN https://localhost:9200?pretty=true
这里的 pretty就是要求咱们要以 JSON 的格局来进行显现。虽然咱们上面的指令能够省去 pretty=true 这个选项,能够仍是能够得到漂亮的输出:
1. curl -k -u elastic:gV4pgxNCTi5y*80GmoqN https://localhost:9200
2. {
3. "name" : "liuxgm.local",
4. "cluster_name" : "elasticsearch",
5. "cluster_uuid" : "xz4jLE_USfmbvkSyvG138w",
6. "version" : {
7. "number" : "8.6.1",
8. "build_flavor" : "default",
9. "build_type" : "tar",
10. "build_hash" : "180c9830da956993e59e2cd70eb32b5e383ea42c",
11. "build_date" : "2023-01-24T21:35:11.506992272Z",
12. "build_snapshot" : false,
13. "lucene_version" : "9.4.2",
14. "minimum_wire_compatibility_version" : "7.17.0",
15. "minimum_index_compatibility_version" : "7.0.0"
16. },
17. "tagline" : "You Know, for Search"
18. }
咱们能够看如下的指令的输出:
curl -k https://localhost:9200?pretty=true
1. curl -k https://localhost:9200?pretty=true
2. {
3. "error" : {
4. "root_cause" : [
5. {
6. "type" : "security_exception",
7. "reason" : "missing authentication credentials for REST request [/?pretty=true]",
8. "header" : {
9. "WWW-Authenticate" : [
10. "Basic realm=\"security\" charset=\"UTF-8\"",
11. "Bearer realm=\"security\"",
12. "ApiKey"
13. ]
14. }
15. }
16. ],
17. "type" : "security_exception",
18. "reason" : "missing authentication credentials for REST request [/?pretty=true]",
19. "header" : {
20. "WWW-Authenticate" : [
21. "Basic realm=\"security\" charset=\"UTF-8\"",
22. "Bearer realm=\"security\"",
23. "ApiKey"
24. ]
25. }
26. },
27. "status" : 401
28. }
咱们能够看到很漂亮的输出。一旦咱们省去 pretty=true 这个选项,那么咱们看看输出的成果是什么:
1. $ curl -k https://localhost:9200
2. {"error":{"root_cause":[{"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":["Basic realm=\"security\" charset=\"UTF-8\"","Bearer realm=\"security\"","ApiKey"]}}],"type":"security_exception","reason":"missing authentication credentials for REST request [/]","header":{"WWW-Authenticate":["Basic realm=\"security\" charset=\"UTF-8\"","Bearer realm=\"security\"","ApiKey"]}},"status":401}$
很显然,咱们的显现十分不漂亮,很难看懂它的意思。
在 Elasticsearch 中,咱们的许多指令都能够运用 pretty=true 选项来变得更加美观。
但是在实际的用例中,有许多并不像 Elasticsearch 那样。它们的指令中并没有像 Elasticsearch 那样供给 pretty=true 这样的选项。那么咱们该怎么办呢?
在下面,我来介绍几种方案:
运用 json_pp
咱们测验运用如下的指令:
echo '{"type":"Bar","id":"1","title":"Foo"}' | json_pp -json_opt pretty,canonical
1. $ echo '{"type":"Bar","id":"1","title":"Foo"}' | json_pp -json_opt pretty,canonical
2. {
3. "id" : "1",
4. "title" : "Foo",
5. "type" : "Bar"
6. }
很显然,它能帮咱们把 JSON 的输出变得很漂亮。咱们来测验一下上面的 Elasticsearch 访问:
1. $ curl -k https://localhost:9200 | json_pp -json_opt pretty,canonical
2. % Total % Received % Xferd Average Speed Time Time Time Current
3. Dload Upload Total Spent Left Speed
4. 100 459 100 459 0 0 29436 0 --:--:-- --:--:-- --:--:-- 38250
5. {
6. "error" : {
7. "header" : {
8. "WWW-Authenticate" : [
9. "Basic realm=\"security\" charset=\"UTF-8\"",
10. "Bearer realm=\"security\"",
11. "ApiKey"
12. ]
13. },
14. "reason" : "missing authentication credentials for REST request [/]",
15. "root_cause" : [
16. {
17. "header" : {
18. "WWW-Authenticate" : [
19. "Basic realm=\"security\" charset=\"UTF-8\"",
20. "Bearer realm=\"security\"",
21. "ApiKey"
22. ]
23. },
24. "reason" : "missing authentication credentials for REST request [/]",
25. "type" : "security_exception"
26. }
27. ],
28. "type" : "security_exception"
29. },
30. "status" : 401
31. }
很显然,它也作业正常。
运用 jq
咱们需要单独装置 jq。咱们运用如下的指令来进行验证:
echo '{"type":"Bar","id":"1","title":"Foo"}' | jq '.'
1. $ echo '{"type":"Bar","id":"1","title":"Foo"}' | jq '.'
2. {
3. "type": "Bar",
4. "id": "1",
5. "title": "Foo"
6. }
显然这种办法比上面的办法更为简洁。咱们也能够运用它来试一下 Elasticsearch 的访问:
curl -k https://localhost:9200 | jq '.'
它还有五颜六色的输出。显得十分漂亮。
运用 python
咱们运用如下的例子来进行展示:
echo '{"type":"Bar","id":"1","title":"Foo"}' | python -m json.tool
1. $ echo '{"type":"Bar","id":"1","title":"Foo"}' | python -m json.tool
2. {
3. "type": "Bar",
4. "id": "1",
5. "title": "Foo"
6. }
测验一下 Elasticsearch API:
它和 jq 输出的成果很类似,除了没有五颜六色的显现。
咱们还能够运用 curljson 指令:
pip install curljson
1. curljson -k https://localhost:9200
2. {
3. "error": {
4. "header": {
5. "WWW-Authenticate": [
6. "Basic realm=\"security\" charset=\"UTF-8\"",
7. "Bearer realm=\"security\"",
8. "ApiKey"
9. ]
10. },
11. "reason": "missing authentication credentials for REST request [/]",
12. "root_cause": [
13. {
14. "header": {
15. "WWW-Authenticate": [
16. "Basic realm=\"security\" charset=\"UTF-8\"",
17. "Bearer realm=\"security\"",
18. "ApiKey"
19. ]
20. },
21. "reason": "missing authentication credentials for REST request [/]",
22. "type": "security_exception"
23. }
24. ],
25. "type": "security_exception"
26. },
27. "status": 401
28. }
运用 Nodejs
echo '{"type":"Bar","id":"1","title":"Foo"}' | node -e "console.log( JSON.stringify( JSON.parse(require('fs').readFileSync(0) ), 0, 1 ))"
1. $ echo '{"type":"Bar","id":"1","title":"Foo"}' | node -e "console.log( JSON.stringify( JSON.parse(require('fs').readFileSync(0) ), 0, 1 ))"
2. {
3. "type": "Bar",
4. "id": "1",
5. "title": "Foo"
6. }
很显然这种办法十分笨拙。你也能够选用如下的办法:
echo '{"foo": "lorem", "bar": "ipsum"}' | npx json
1. echo '{"foo": "lorem", "bar": "ipsum"}' | npx json
2. {
3. "foo": "lorem",
4. "bar": "ipsum"
5. }
运用 json_reformat
在 Linux 机器上装置yajl-tools 装置包,然后运用如下的指令:
1. echo '{"foo": "lorem", "bar": "ipsum"}' | json_reformat
2. {
3. "foo": "lorem",
4. "bar": "ipsum"
5. }