|
| 1 | +.. _json-path-output-option: |
| 2 | + |
| 3 | +========================= |
| 4 | +``json-path`` Output Type |
| 5 | +========================= |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecol |
| 14 | + |
| 15 | +The ``json-path`` output type limits the results of a command to the |
| 16 | +fields you specify. |
| 17 | + |
| 18 | +Usage |
| 19 | +----- |
| 20 | + |
| 21 | +When you add the ``--output`` option to a command, you can specify the |
| 22 | +type ``json-path``. You must provide ``json-path`` with an expression |
| 23 | +to evaluate against your results, which means you must be aware of the |
| 24 | +``json`` fields returned by your command. |
| 25 | + |
| 26 | +Syntax |
| 27 | +~~~~~~ |
| 28 | + |
| 29 | +.. code-block:: shell |
| 30 | + :copyable: false |
| 31 | + |
| 32 | + <command> --output|-o json-path='$<expression>' |
| 33 | + |
| 34 | +``json-path`` expressions refer to the |json| element that a |mcli| |
| 35 | +command returns. The ``$`` character represents the root element, which |
| 36 | +is usually an object or an array. |
| 37 | + |
| 38 | +For a list of valid characters and their functions, see `JSONPath expressions <https://goessner.net/articles/JsonPath/index.html#e2>`__. |
| 39 | + |
| 40 | +Examples |
| 41 | +-------- |
| 42 | + |
| 43 | +Return the description of the first |api| key in a list |
| 44 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 45 | + |
| 46 | +In the following example, a user retrieves their |api| keys with |
| 47 | +:ref:`mongocli-iam-organizations-apiKeys-list`. The ``json-path`` |
| 48 | +expression limits the output to the ``desc`` field of the first key, |
| 49 | +rather than returning the entire list of keys. |
| 50 | + |
| 51 | +.. code-block:: shell |
| 52 | + :copyable: false |
| 53 | + |
| 54 | + mongocli iam organization apikeys list --output json-path='$[0].desc' |
| 55 | + > owner_key |
| 56 | + |
| 57 | +Running the same command with ``--output json`` returns the full |json| |
| 58 | +element from the |api|. It's important to understand the |json| |
| 59 | +structure returned by a command in order to operate on it with |
| 60 | +``json-path``. |
| 61 | + |
| 62 | +Using the the following full |json| output for reference, the |
| 63 | +``--output json-path`` with the expression ``$[0].desc`` finds and |
| 64 | +returns only the value ``"owner_key"``: |
| 65 | + |
| 66 | +.. code-block:: json |
| 67 | + :copyable: false |
| 68 | + :emphasize-lines: 1-2, 4 |
| 69 | + |
| 70 | + [ //``$`` represents the outer array. |
| 71 | + { // ``[0]`` refers to the first element in the array (using a 0-based index). |
| 72 | + "id": "60e736a95d585d2c9ccf2d19", |
| 73 | + "desc": "owner_key", //``.desc`` refers to the ``desc`` field of that element. |
| 74 | + "roles": [ |
| 75 | + { |
| 76 | + "orgId": "5d961a949ccf64b4e7f53bac", |
| 77 | + "roleName": "ORG_OWNER" |
| 78 | + } |
| 79 | + ], |
| 80 | + "privateKey": "********-****-****-c4e26334754f", |
| 81 | + "publicKey": "xtfmtguk" |
| 82 | + }, |
| 83 | + { |
| 84 | + "id": "d2c9ccf2d1960e736a95d585", |
| 85 | + "desc": "member_key", |
| 86 | + "roles": [ |
| 87 | + { |
| 88 | + "orgId": "5d961a949ccf64b4e7f53bac", |
| 89 | + "roleName": "ORG_MEMBER" |
| 90 | + } |
| 91 | + ], |
| 92 | + "privateKey": "********-****-****-c4e26334754f", |
| 93 | + "publicKey": "vfgcttku" |
| 94 | + }, |
| 95 | + ] |
| 96 | + |
| 97 | +Return the description of a specific |api| key in a list |
| 98 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 99 | + |
| 100 | +In the following example, a user retrieves their |api| keys with |
| 101 | +:ref:`mongocli-iam-organizations-apiKeys-list`. The ``json-path`` |
| 102 | +expression limits the output to the ``desc`` field of the specific |
| 103 | +|json| object with ``id`` ``d2c9ccf2d1960e736a95d585``. |
| 104 | + |
| 105 | +.. code-block:: shell |
| 106 | + :copyable: false |
| 107 | + |
| 108 | + mongocli iam organization apikeys list --output json-path='$[? @.id=="d2c9ccf2d1960e736a95d585"].desc' |
| 109 | + > member_key |
| 110 | + |
| 111 | +Return the status a private endpoint |
| 112 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 113 | + |
| 114 | +In the following example, a user retrieves information for a |
| 115 | +private endpoint with |
| 116 | +:ref:`mongocli-atlas-privateEndpoints-aws-describe`. The ``json-path`` |
| 117 | +expression limits the output to the ``status`` field of the root |
| 118 | +element. |
| 119 | + |
| 120 | +.. code-block:: shell |
| 121 | + :copyable: false |
| 122 | + |
| 123 | + mongocli atlas privateendpoint aws describe 601a4044da900269480a2533 --output json-path='$.status' |
| 124 | + > WAITING_FOR_USER |
0 commit comments