Como analisar JSON com script de shell para o caso de borda de chaves

0

Eu tenho uma saída JSON da qual preciso extrair alguns parâmetros no Linux.

Esta é a saída JSON:

{
  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"
    }

Eu quero a saída de

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

Eu já tentei com abaixo, mas não estou trabalhando:

file.txt | python -mjson.tool | grep 'dataset_key'
    
por user3256289 24.09.2018 / 14:16

2 respostas

4

Supondo que o documento JSON esteja bem formado e completo, como por exemplo

{
  "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"
    }
  ]
}

A segunda matriz item do elemento dataset_key pode ser obtida com jq :

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

Altere [1] para [-1] para obter o dataset_key do elemento último item .

Para obter os dados brutos dos elementos da matriz:

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

Se você não puder tornar o json válido, por exemplo Para uma saída de uma API da qual você não tem controle, isso retornará a saída desejada:

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

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

    
por 24.09.2018 / 14:49

Tags