How to parse JSON with shell scripting for braces edge case












0















I have a JSON output from which I need to extract a few parameters in Linux.



This is the JSON output:



{
items:[
{
provider_name:"ucp-ipg",
subject_name:"rtm-instrumentation",
dataset_name:"rtm-instrumentation-dataset-hour-sliced",
dataset_key:[
2018-03-06T06:00:00Z,
"000394e3-a9eb-40b6-9463-fbd588d20ba4"
],
record_count:21,
state:"complete",
version:0,
etag:"a221df62",
creation_timestamp:2018-03-06T06:10:46.294-00:00,
created_by:"AAA",
modification_timestamp:2018-03-06T06:10:46.294-00:00,
modified_by:"AAA"
},
{
provider_name:"ucp-ipg",
subject_name:"rtm-instrumentation",
dataset_name:"rtm-instrumentation-dataset-hour-sliced",
dataset_key:[
2018-03-06T06:00:00Z,
"00097722-b02f-4938-bd4b-d935047c3837"
],
record_count:17,
state:"complete",
version:0,
etag:"aa4dbc25",
creation_timestamp:2018-03-06T06:12:23.293-00:00,
created_by:"AAA",
modification_timestamp:2018-03-06T06:12:23.293-00:00,
modified_by:"AAA"
}


I want the output of



dataset_key:[
2018-03-06T06:00:00Z,
"00097722-b02f-4938-bd4b-d935047c3837"
]




I have already tried with below but not working :



file.txt | python -mjson.tool | grep 'dataset_key'









share|improve this question




















  • 3





    This does not seem to be a well formed JSON document. The keys are not quoted and some of the data is not quoted.

    – Kusalananda
    Sep 24 '18 at 12:30








  • 1





    And not only are the keys not quoted, the dates (if they are supposed to be strings) are also not quoted. So the first step would be to get whatever produces this output to produce proper JSON, so you can use JSON tools like jq on it.

    – dirkt
    Sep 24 '18 at 12:34











  • You are mixing json processing and grep processing: the python -mjson.tool just passes the input to the output, and grep works one one line at a time. You need to tell the json tool to do the grepping.

    – ctrl-alt-delor
    Sep 24 '18 at 16:03
















0















I have a JSON output from which I need to extract a few parameters in Linux.



This is the JSON output:



{
items:[
{
provider_name:"ucp-ipg",
subject_name:"rtm-instrumentation",
dataset_name:"rtm-instrumentation-dataset-hour-sliced",
dataset_key:[
2018-03-06T06:00:00Z,
"000394e3-a9eb-40b6-9463-fbd588d20ba4"
],
record_count:21,
state:"complete",
version:0,
etag:"a221df62",
creation_timestamp:2018-03-06T06:10:46.294-00:00,
created_by:"AAA",
modification_timestamp:2018-03-06T06:10:46.294-00:00,
modified_by:"AAA"
},
{
provider_name:"ucp-ipg",
subject_name:"rtm-instrumentation",
dataset_name:"rtm-instrumentation-dataset-hour-sliced",
dataset_key:[
2018-03-06T06:00:00Z,
"00097722-b02f-4938-bd4b-d935047c3837"
],
record_count:17,
state:"complete",
version:0,
etag:"aa4dbc25",
creation_timestamp:2018-03-06T06:12:23.293-00:00,
created_by:"AAA",
modification_timestamp:2018-03-06T06:12:23.293-00:00,
modified_by:"AAA"
}


I want the output of



dataset_key:[
2018-03-06T06:00:00Z,
"00097722-b02f-4938-bd4b-d935047c3837"
]




I have already tried with below but not working :



file.txt | python -mjson.tool | grep 'dataset_key'









share|improve this question




















  • 3





    This does not seem to be a well formed JSON document. The keys are not quoted and some of the data is not quoted.

    – Kusalananda
    Sep 24 '18 at 12:30








  • 1





    And not only are the keys not quoted, the dates (if they are supposed to be strings) are also not quoted. So the first step would be to get whatever produces this output to produce proper JSON, so you can use JSON tools like jq on it.

    – dirkt
    Sep 24 '18 at 12:34











  • You are mixing json processing and grep processing: the python -mjson.tool just passes the input to the output, and grep works one one line at a time. You need to tell the json tool to do the grepping.

    – ctrl-alt-delor
    Sep 24 '18 at 16:03














0












0








0








I have a JSON output from which I need to extract a few parameters in Linux.



This is the JSON output:



{
items:[
{
provider_name:"ucp-ipg",
subject_name:"rtm-instrumentation",
dataset_name:"rtm-instrumentation-dataset-hour-sliced",
dataset_key:[
2018-03-06T06:00:00Z,
"000394e3-a9eb-40b6-9463-fbd588d20ba4"
],
record_count:21,
state:"complete",
version:0,
etag:"a221df62",
creation_timestamp:2018-03-06T06:10:46.294-00:00,
created_by:"AAA",
modification_timestamp:2018-03-06T06:10:46.294-00:00,
modified_by:"AAA"
},
{
provider_name:"ucp-ipg",
subject_name:"rtm-instrumentation",
dataset_name:"rtm-instrumentation-dataset-hour-sliced",
dataset_key:[
2018-03-06T06:00:00Z,
"00097722-b02f-4938-bd4b-d935047c3837"
],
record_count:17,
state:"complete",
version:0,
etag:"aa4dbc25",
creation_timestamp:2018-03-06T06:12:23.293-00:00,
created_by:"AAA",
modification_timestamp:2018-03-06T06:12:23.293-00:00,
modified_by:"AAA"
}


I want the output of



dataset_key:[
2018-03-06T06:00:00Z,
"00097722-b02f-4938-bd4b-d935047c3837"
]




I have already tried with below but not working :



file.txt | python -mjson.tool | grep 'dataset_key'









share|improve this question
















I have a JSON output from which I need to extract a few parameters in Linux.



This is the JSON output:



{
items:[
{
provider_name:"ucp-ipg",
subject_name:"rtm-instrumentation",
dataset_name:"rtm-instrumentation-dataset-hour-sliced",
dataset_key:[
2018-03-06T06:00:00Z,
"000394e3-a9eb-40b6-9463-fbd588d20ba4"
],
record_count:21,
state:"complete",
version:0,
etag:"a221df62",
creation_timestamp:2018-03-06T06:10:46.294-00:00,
created_by:"AAA",
modification_timestamp:2018-03-06T06:10:46.294-00:00,
modified_by:"AAA"
},
{
provider_name:"ucp-ipg",
subject_name:"rtm-instrumentation",
dataset_name:"rtm-instrumentation-dataset-hour-sliced",
dataset_key:[
2018-03-06T06:00:00Z,
"00097722-b02f-4938-bd4b-d935047c3837"
],
record_count:17,
state:"complete",
version:0,
etag:"aa4dbc25",
creation_timestamp:2018-03-06T06:12:23.293-00:00,
created_by:"AAA",
modification_timestamp:2018-03-06T06:12:23.293-00:00,
modified_by:"AAA"
}


I want the output of



dataset_key:[
2018-03-06T06:00:00Z,
"00097722-b02f-4938-bd4b-d935047c3837"
]




I have already tried with below but not working :



file.txt | python -mjson.tool | grep 'dataset_key'






linux json






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 24 '18 at 12:46









Kusalananda

124k16236388




124k16236388










asked Sep 24 '18 at 12:16









user3256289user3256289

1




1








  • 3





    This does not seem to be a well formed JSON document. The keys are not quoted and some of the data is not quoted.

    – Kusalananda
    Sep 24 '18 at 12:30








  • 1





    And not only are the keys not quoted, the dates (if they are supposed to be strings) are also not quoted. So the first step would be to get whatever produces this output to produce proper JSON, so you can use JSON tools like jq on it.

    – dirkt
    Sep 24 '18 at 12:34











  • You are mixing json processing and grep processing: the python -mjson.tool just passes the input to the output, and grep works one one line at a time. You need to tell the json tool to do the grepping.

    – ctrl-alt-delor
    Sep 24 '18 at 16:03














  • 3





    This does not seem to be a well formed JSON document. The keys are not quoted and some of the data is not quoted.

    – Kusalananda
    Sep 24 '18 at 12:30








  • 1





    And not only are the keys not quoted, the dates (if they are supposed to be strings) are also not quoted. So the first step would be to get whatever produces this output to produce proper JSON, so you can use JSON tools like jq on it.

    – dirkt
    Sep 24 '18 at 12:34











  • You are mixing json processing and grep processing: the python -mjson.tool just passes the input to the output, and grep works one one line at a time. You need to tell the json tool to do the grepping.

    – ctrl-alt-delor
    Sep 24 '18 at 16:03








3




3





This does not seem to be a well formed JSON document. The keys are not quoted and some of the data is not quoted.

– Kusalananda
Sep 24 '18 at 12:30







This does not seem to be a well formed JSON document. The keys are not quoted and some of the data is not quoted.

– Kusalananda
Sep 24 '18 at 12:30






1




1





And not only are the keys not quoted, the dates (if they are supposed to be strings) are also not quoted. So the first step would be to get whatever produces this output to produce proper JSON, so you can use JSON tools like jq on it.

– dirkt
Sep 24 '18 at 12:34





And not only are the keys not quoted, the dates (if they are supposed to be strings) are also not quoted. So the first step would be to get whatever produces this output to produce proper JSON, so you can use JSON tools like jq on it.

– dirkt
Sep 24 '18 at 12:34













You are mixing json processing and grep processing: the python -mjson.tool just passes the input to the output, and grep works one one line at a time. You need to tell the json tool to do the grepping.

– ctrl-alt-delor
Sep 24 '18 at 16:03





You are mixing json processing and grep processing: the python -mjson.tool just passes the input to the output, and grep works one one line at a time. You need to tell the json tool to do the grepping.

– ctrl-alt-delor
Sep 24 '18 at 16:03










3 Answers
3






active

oldest

votes


















4














Assuming that the JSON document is well formed and complete, as for example



{
"items": [
{
"provider_name": "ucp-ipg",
"subject_name": "rtm-instrumentation",
"dataset_name": "rtm-instrumentation-dataset-hour-sliced",
"dataset_key": [
"2018-03-06T06:00:00Z",
"000394e3-a9eb-40b6-9463-fbd588d20ba4"
],
"record_count": 21,
"state": "complete",
"version": 0,
"etag": "a221df62",
"creation_timestamp": "2018-03-06T06:10:46.294-00:00",
"created_by": "AAA",
"modification_timestamp": "2018-03-06T06:10:46.294-00:00",
"modified_by": "AAA"
},
{
"provider_name": "ucp-ipg",
"subject_name": "rtm-instrumentation",
"dataset_name": "rtm-instrumentation-dataset-hour-sliced",
"dataset_key": [
"2018-03-06T06:00:00Z",
"00097722-b02f-4938-bd4b-d935047c3837"
],
"record_count": 17,
"state": "complete",
"version": 0,
"etag": "aa4dbc25",
"creation_timestamp": "2018-03-06T06:12:23.293-00:00",
"created_by": "AAA",
"modification_timestamp": "2018-03-06T06:12:23.293-00:00",
"modified_by": "AAA"
}
]
}


The second item element's dataset_key array may be had with jq:



$ jq '.items[1].dataset_key' file.json
[
"2018-03-06T06:00:00Z",
"00097722-b02f-4938-bd4b-d935047c3837"
]


Change [1] to [-1] to get the dataset_key from the last item element.



To get the raw data of the array elements:



$ jq -r '.items[1].dataset_key' file.json
2018-03-06T06:00:00Z
00097722-b02f-4938-bd4b-d935047c3837





share|improve this answer


























  • wow , works perfectly. Thanks

    – user3256289
    Sep 24 '18 at 13:04








  • 1





    @user3256289: If this works for you, then the example you gave in your question doesn't match your actual data, because AFAIK jq doesn't parse malformed JSON. So, please include the correct data the next time.

    – dirkt
    Sep 24 '18 at 13:37



















0














If you cannot make the json valid, e.g. for an output of an API you have no control of, this will return your desired output:



perl -0777 -ne '/(dataset_key:[[^]]*])/ && print "$1n"' file.txt



Note: This would break if the item contained a ].







share|improve this answer

































    0














    an easy way to extract info from json is jtc (assuming your json is fixed):



    bash $ jtc -w '<dataset_key>l+0' -r your.json 
    [ "2018-03-06T06:00:00Z", "000394e3-a9eb-40b6-9463-fbd588d20ba4" ]
    [ "2018-03-06T06:00:00Z", "00097722-b02f-4938-bd4b-d935047c3837" ]
    bash $





    share|improve this answer























      Your Answer








      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "106"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: false,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471066%2fhow-to-parse-json-with-shell-scripting-for-braces-edge-case%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      4














      Assuming that the JSON document is well formed and complete, as for example



      {
      "items": [
      {
      "provider_name": "ucp-ipg",
      "subject_name": "rtm-instrumentation",
      "dataset_name": "rtm-instrumentation-dataset-hour-sliced",
      "dataset_key": [
      "2018-03-06T06:00:00Z",
      "000394e3-a9eb-40b6-9463-fbd588d20ba4"
      ],
      "record_count": 21,
      "state": "complete",
      "version": 0,
      "etag": "a221df62",
      "creation_timestamp": "2018-03-06T06:10:46.294-00:00",
      "created_by": "AAA",
      "modification_timestamp": "2018-03-06T06:10:46.294-00:00",
      "modified_by": "AAA"
      },
      {
      "provider_name": "ucp-ipg",
      "subject_name": "rtm-instrumentation",
      "dataset_name": "rtm-instrumentation-dataset-hour-sliced",
      "dataset_key": [
      "2018-03-06T06:00:00Z",
      "00097722-b02f-4938-bd4b-d935047c3837"
      ],
      "record_count": 17,
      "state": "complete",
      "version": 0,
      "etag": "aa4dbc25",
      "creation_timestamp": "2018-03-06T06:12:23.293-00:00",
      "created_by": "AAA",
      "modification_timestamp": "2018-03-06T06:12:23.293-00:00",
      "modified_by": "AAA"
      }
      ]
      }


      The second item element's dataset_key array may be had with jq:



      $ jq '.items[1].dataset_key' file.json
      [
      "2018-03-06T06:00:00Z",
      "00097722-b02f-4938-bd4b-d935047c3837"
      ]


      Change [1] to [-1] to get the dataset_key from the last item element.



      To get the raw data of the array elements:



      $ jq -r '.items[1].dataset_key' file.json
      2018-03-06T06:00:00Z
      00097722-b02f-4938-bd4b-d935047c3837





      share|improve this answer


























      • wow , works perfectly. Thanks

        – user3256289
        Sep 24 '18 at 13:04








      • 1





        @user3256289: If this works for you, then the example you gave in your question doesn't match your actual data, because AFAIK jq doesn't parse malformed JSON. So, please include the correct data the next time.

        – dirkt
        Sep 24 '18 at 13:37
















      4














      Assuming that the JSON document is well formed and complete, as for example



      {
      "items": [
      {
      "provider_name": "ucp-ipg",
      "subject_name": "rtm-instrumentation",
      "dataset_name": "rtm-instrumentation-dataset-hour-sliced",
      "dataset_key": [
      "2018-03-06T06:00:00Z",
      "000394e3-a9eb-40b6-9463-fbd588d20ba4"
      ],
      "record_count": 21,
      "state": "complete",
      "version": 0,
      "etag": "a221df62",
      "creation_timestamp": "2018-03-06T06:10:46.294-00:00",
      "created_by": "AAA",
      "modification_timestamp": "2018-03-06T06:10:46.294-00:00",
      "modified_by": "AAA"
      },
      {
      "provider_name": "ucp-ipg",
      "subject_name": "rtm-instrumentation",
      "dataset_name": "rtm-instrumentation-dataset-hour-sliced",
      "dataset_key": [
      "2018-03-06T06:00:00Z",
      "00097722-b02f-4938-bd4b-d935047c3837"
      ],
      "record_count": 17,
      "state": "complete",
      "version": 0,
      "etag": "aa4dbc25",
      "creation_timestamp": "2018-03-06T06:12:23.293-00:00",
      "created_by": "AAA",
      "modification_timestamp": "2018-03-06T06:12:23.293-00:00",
      "modified_by": "AAA"
      }
      ]
      }


      The second item element's dataset_key array may be had with jq:



      $ jq '.items[1].dataset_key' file.json
      [
      "2018-03-06T06:00:00Z",
      "00097722-b02f-4938-bd4b-d935047c3837"
      ]


      Change [1] to [-1] to get the dataset_key from the last item element.



      To get the raw data of the array elements:



      $ jq -r '.items[1].dataset_key' file.json
      2018-03-06T06:00:00Z
      00097722-b02f-4938-bd4b-d935047c3837





      share|improve this answer


























      • wow , works perfectly. Thanks

        – user3256289
        Sep 24 '18 at 13:04








      • 1





        @user3256289: If this works for you, then the example you gave in your question doesn't match your actual data, because AFAIK jq doesn't parse malformed JSON. So, please include the correct data the next time.

        – dirkt
        Sep 24 '18 at 13:37














      4












      4








      4







      Assuming that the JSON document is well formed and complete, as for example



      {
      "items": [
      {
      "provider_name": "ucp-ipg",
      "subject_name": "rtm-instrumentation",
      "dataset_name": "rtm-instrumentation-dataset-hour-sliced",
      "dataset_key": [
      "2018-03-06T06:00:00Z",
      "000394e3-a9eb-40b6-9463-fbd588d20ba4"
      ],
      "record_count": 21,
      "state": "complete",
      "version": 0,
      "etag": "a221df62",
      "creation_timestamp": "2018-03-06T06:10:46.294-00:00",
      "created_by": "AAA",
      "modification_timestamp": "2018-03-06T06:10:46.294-00:00",
      "modified_by": "AAA"
      },
      {
      "provider_name": "ucp-ipg",
      "subject_name": "rtm-instrumentation",
      "dataset_name": "rtm-instrumentation-dataset-hour-sliced",
      "dataset_key": [
      "2018-03-06T06:00:00Z",
      "00097722-b02f-4938-bd4b-d935047c3837"
      ],
      "record_count": 17,
      "state": "complete",
      "version": 0,
      "etag": "aa4dbc25",
      "creation_timestamp": "2018-03-06T06:12:23.293-00:00",
      "created_by": "AAA",
      "modification_timestamp": "2018-03-06T06:12:23.293-00:00",
      "modified_by": "AAA"
      }
      ]
      }


      The second item element's dataset_key array may be had with jq:



      $ jq '.items[1].dataset_key' file.json
      [
      "2018-03-06T06:00:00Z",
      "00097722-b02f-4938-bd4b-d935047c3837"
      ]


      Change [1] to [-1] to get the dataset_key from the last item element.



      To get the raw data of the array elements:



      $ jq -r '.items[1].dataset_key' file.json
      2018-03-06T06:00:00Z
      00097722-b02f-4938-bd4b-d935047c3837





      share|improve this answer















      Assuming that the JSON document is well formed and complete, as for example



      {
      "items": [
      {
      "provider_name": "ucp-ipg",
      "subject_name": "rtm-instrumentation",
      "dataset_name": "rtm-instrumentation-dataset-hour-sliced",
      "dataset_key": [
      "2018-03-06T06:00:00Z",
      "000394e3-a9eb-40b6-9463-fbd588d20ba4"
      ],
      "record_count": 21,
      "state": "complete",
      "version": 0,
      "etag": "a221df62",
      "creation_timestamp": "2018-03-06T06:10:46.294-00:00",
      "created_by": "AAA",
      "modification_timestamp": "2018-03-06T06:10:46.294-00:00",
      "modified_by": "AAA"
      },
      {
      "provider_name": "ucp-ipg",
      "subject_name": "rtm-instrumentation",
      "dataset_name": "rtm-instrumentation-dataset-hour-sliced",
      "dataset_key": [
      "2018-03-06T06:00:00Z",
      "00097722-b02f-4938-bd4b-d935047c3837"
      ],
      "record_count": 17,
      "state": "complete",
      "version": 0,
      "etag": "aa4dbc25",
      "creation_timestamp": "2018-03-06T06:12:23.293-00:00",
      "created_by": "AAA",
      "modification_timestamp": "2018-03-06T06:12:23.293-00:00",
      "modified_by": "AAA"
      }
      ]
      }


      The second item element's dataset_key array may be had with jq:



      $ jq '.items[1].dataset_key' file.json
      [
      "2018-03-06T06:00:00Z",
      "00097722-b02f-4938-bd4b-d935047c3837"
      ]


      Change [1] to [-1] to get the dataset_key from the last item element.



      To get the raw data of the array elements:



      $ jq -r '.items[1].dataset_key' file.json
      2018-03-06T06:00:00Z
      00097722-b02f-4938-bd4b-d935047c3837






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Sep 24 '18 at 12:49

























      answered Sep 24 '18 at 12:36









      KusalanandaKusalananda

      124k16236388




      124k16236388













      • wow , works perfectly. Thanks

        – user3256289
        Sep 24 '18 at 13:04








      • 1





        @user3256289: If this works for you, then the example you gave in your question doesn't match your actual data, because AFAIK jq doesn't parse malformed JSON. So, please include the correct data the next time.

        – dirkt
        Sep 24 '18 at 13:37



















      • wow , works perfectly. Thanks

        – user3256289
        Sep 24 '18 at 13:04








      • 1





        @user3256289: If this works for you, then the example you gave in your question doesn't match your actual data, because AFAIK jq doesn't parse malformed JSON. So, please include the correct data the next time.

        – dirkt
        Sep 24 '18 at 13:37

















      wow , works perfectly. Thanks

      – user3256289
      Sep 24 '18 at 13:04







      wow , works perfectly. Thanks

      – user3256289
      Sep 24 '18 at 13:04






      1




      1





      @user3256289: If this works for you, then the example you gave in your question doesn't match your actual data, because AFAIK jq doesn't parse malformed JSON. So, please include the correct data the next time.

      – dirkt
      Sep 24 '18 at 13:37





      @user3256289: If this works for you, then the example you gave in your question doesn't match your actual data, because AFAIK jq doesn't parse malformed JSON. So, please include the correct data the next time.

      – dirkt
      Sep 24 '18 at 13:37













      0














      If you cannot make the json valid, e.g. for an output of an API you have no control of, this will return your desired output:



      perl -0777 -ne '/(dataset_key:[[^]]*])/ && print "$1n"' file.txt



      Note: This would break if the item contained a ].







      share|improve this answer






























        0














        If you cannot make the json valid, e.g. for an output of an API you have no control of, this will return your desired output:



        perl -0777 -ne '/(dataset_key:[[^]]*])/ && print "$1n"' file.txt



        Note: This would break if the item contained a ].







        share|improve this answer




























          0












          0








          0







          If you cannot make the json valid, e.g. for an output of an API you have no control of, this will return your desired output:



          perl -0777 -ne '/(dataset_key:[[^]]*])/ && print "$1n"' file.txt



          Note: This would break if the item contained a ].







          share|improve this answer















          If you cannot make the json valid, e.g. for an output of an API you have no control of, this will return your desired output:



          perl -0777 -ne '/(dataset_key:[[^]]*])/ && print "$1n"' file.txt



          Note: This would break if the item contained a ].








          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Sep 24 '18 at 12:54

























          answered Sep 24 '18 at 12:49









          RoVoRoVo

          2,773216




          2,773216























              0














              an easy way to extract info from json is jtc (assuming your json is fixed):



              bash $ jtc -w '<dataset_key>l+0' -r your.json 
              [ "2018-03-06T06:00:00Z", "000394e3-a9eb-40b6-9463-fbd588d20ba4" ]
              [ "2018-03-06T06:00:00Z", "00097722-b02f-4938-bd4b-d935047c3837" ]
              bash $





              share|improve this answer




























                0














                an easy way to extract info from json is jtc (assuming your json is fixed):



                bash $ jtc -w '<dataset_key>l+0' -r your.json 
                [ "2018-03-06T06:00:00Z", "000394e3-a9eb-40b6-9463-fbd588d20ba4" ]
                [ "2018-03-06T06:00:00Z", "00097722-b02f-4938-bd4b-d935047c3837" ]
                bash $





                share|improve this answer


























                  0












                  0








                  0







                  an easy way to extract info from json is jtc (assuming your json is fixed):



                  bash $ jtc -w '<dataset_key>l+0' -r your.json 
                  [ "2018-03-06T06:00:00Z", "000394e3-a9eb-40b6-9463-fbd588d20ba4" ]
                  [ "2018-03-06T06:00:00Z", "00097722-b02f-4938-bd4b-d935047c3837" ]
                  bash $





                  share|improve this answer













                  an easy way to extract info from json is jtc (assuming your json is fixed):



                  bash $ jtc -w '<dataset_key>l+0' -r your.json 
                  [ "2018-03-06T06:00:00Z", "000394e3-a9eb-40b6-9463-fbd588d20ba4" ]
                  [ "2018-03-06T06:00:00Z", "00097722-b02f-4938-bd4b-d935047c3837" ]
                  bash $






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 9 at 18:33









                  Dmitry L.Dmitry L.

                  112




                  112






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Unix & Linux Stack Exchange!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f471066%2fhow-to-parse-json-with-shell-scripting-for-braces-edge-case%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Morgemoulin

                      Scott Moir

                      Souastre