使用jq将地图列表转换为数组

时间:2019-06-06 15:39:34

标签: arrays json amazon-dynamodb transformation jq

我正在努力从aws dynamodb扫描命令获取格式化输出。 dynamodb表中的一项如下所示:

{
   "labels": {
    "Category": [
      "Data",
      "EMR"
    ],
   "Environment": "NonProd",
   "Severity": "Critical"
   },
  "subscriber_id": "blah@blah.com",
  "subscriber_type": "email"
}

运行查询时:

aws dynamodb scan --table-name dummy_table --region region_name -- 
profile default --query "Items[?subscriber_id.S == 
'blah@blah.com'].labels.M[]"

我得到的输出如下:

[
{
    "Environment": {
        "S": "NonProd"
    },
    "Severity": {
        "S": "Critical"
    },
       "Category": {
            "L": [
                {
                   "S": "Data"
                },
               {
                    "S": "EMR"
               }
            ]
       }
    }
]

所需的输出是:

    {
        "Category": [
        "Data",
        "EMR"
        ],
       "Environment": "NonProd",
       "Severity": "Critical"
    }

为了获得所需的输出,我尝试使用jq进行操作。

更新的查询:

    aws dynamodb scan --table-name dummy_table --region 
 region_name -- 
   profile default --query "Items[?subscriber_id.S == 
   'blah@blah.com'].labels.M[]" |
   jq -r '.[] 
     | to_entries[] 
     | [{key:.key, value:.value[]}] 
     | from_entries' | jq -s add

输出为:

{
  "Environment": "NonProd",
  "Severity": "Critical",
  "Category": [
    {
      "S": "Data"
    },
    {
      "S": "EMR"
    }
  ]
}

如您所见,它已经关闭,但是没有处理类别列表。感谢获得所需输出的任何帮助。

谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用以下内容:

new Thread(new Runnable() {

            @Override
            public void run() {

                int sampleRate = 22050;
                int channelConfig = AudioFormat.CHANNEL_IN_MONO;
                int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

                int bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat);

                AudioRecord recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, 2048);
                recorder.startRecording();

                short[] audioData = new short[1024];
                float[] fAudioData = new float[1024];

                Yin yin = new Yin(sampleRate, 1024);

                while(true) {

                    recorder.read(audioData, 0, 1024);

                    for (int i = 0; i < audioData.length; i++) {

                        fAudioData[i] = (float) audioData[i];

                    }

                    float pitch = yin.getPitch(fAudioData).getPitch();

                    if (pitch > 0) {

                        System.out.println(pitch);

                    }

                }


            }

        }).start();

输出:

.[] | { Environment: .Environment.S, Severity: .Severity.S, Category: (.Category.L | map(.S)) }

Try it here