在JSON数据类型

时间:2018-05-19 15:01:20

标签: postgresql postgresql-9.6

我正在使用Kairos的回复,我有那个:

{
   "images":[
      {
         "file":"foo.jpg",
         "faces":[
            {
               "yaw":1,
               "roll":1,
               "pitch":-8,
               "width":113,
               "height":113,
               "face_id":1,
               "quality":0.13918,
               "chinTipX":107,
               "chinTipY":215,
               "topLeftX":52,
               "topLeftY":95,
               "attributes":{
                  "age":31,
                  "lips":"Together",
                  "asian":0.00125,
                  "black":0.0001,
                  "other":0.00571,
                  "white":0.98087,
                  "gender":{
                     "type":"M",
                     "maleConfidence":0.99999,
                     "femaleConfidence":0.00001
                  },
                  "glasses":"None",
                  "hispanic":0.01207
               },
               "confidence":0.99946,
               "eyeDistance":48,
               "leftEyeCenterX":133,
               "leftEyeCenterY":126,
               "rightEyeCenterX":85,
               "rightEyeCenterY":125
            }
         ],
         "width":214,
         "height":317,
         "status":"Complete"
      }
   ]
}

我可以使用此查询选择此json的一小部分:

select facedata.data->'images'->0->'faces'->0->'attributes' 
from facedata 
where facedata.data->'Errors' isnull;

但我想只需要这个小部分的五个关键点; 黑色白色西班牙裔亚洲其他以便能够找到这些键的最大值。

如何选择JSON的多个键并找到最大值?

1 个答案:

答案 0 :(得分:0)

您是否在一张照片中搜索最大值?然后使用json_each()尝试此操作。

SELECT each.key::text,
       each.value::text::decimal
       FROM facedata
            CROSS JOIN json_each(facedata.data->'images'->0->'faces'->0->'attributes') each
       WHERE facedata.data->'Errors' isnull
             AND each.key IN ('black',
                              'white',
                              'hispanic',
                              'asian',
                              'other')
             AND each.value::text::decimal = (SELECT max(eachi.value::text::decimal)
                                                     FROM json_each(facedata.data->'images'->0->'faces'->0->'attributes') eachi
                                                     WHERE eachi.key IN ('black',
                                                                         'white',
                                                                         'hispanic',
                                                                         'asian',
                                                                         'other'));

(注意:如果有的话,它会找到联系。如果你不想要,你可能需要调整它。)

或者是几张照片中的最大值?然后你可能想试试这个。

SELECT max((facedata.data->'images'->0->'faces'->0->'attributes'->'black')::text::decimal) black,
       max((facedata.data->'images'->0->'faces'->0->'attributes'->'white')::text::decimal),
       max((facedata.data->'images'->0->'faces'->0->'attributes'->'hispanic')::text::decimal),
       max((facedata.data->'images'->0->'faces'->0->'attributes'->'asian')::text::decimal),
       max((facedata.data->'images'->0->'faces'->0->'attributes'->'other')::text::decimal)
       FROM facedata
       WHERE facedata.data->'Errors' isnull;