2

I have this JSON :

{
    "duration": 1942,
    "frame_id": 0,
    "detect1": [
        {
            "type": {
                "s_type1": [
                    {
                        "confidence": 98.70016,
                        "klass": -1,
                        "name": "c*****"
                    },
                    {
                        "confidence": 1.042385,
                        "klass": -1,
                        "name": "c*****"
                    },
                    {
                        "confidence": 0.1587732,
                        "klass": -1,
                        "name": "s*****"
                    }
                ],
                "s_type2": [
                    {
                        "confidence": 92.82484,
                        "klass": -1,
                        "name": "b*****"
                    },
                    {
                        "confidence": 7.098834,
                        "klass": -1,
                        "name": "b*****"
                    },
                    {
                        "confidence": 0.02423214,
                        "klass": -1,
                        "name": "p*****"
                    },

                ],
                "Box": [
                    80.80994,
                    170.0965,
                    1091.778
                ]
            },
            "confidences": [
                90.08681,
                99.91595,
                90.12489

            ]
        }
    ]
}

And i would like to save some of key and values from this JSON to another JSON. The new JSON will keep :

  • duration (k,v),
  • frame_id (k,v),
  • detect1 :
    • type : s_type1 and s_type2 only the first dict will be keped and the klass (k,v) will be removed,
    • Box (k,v)
  • confidences (k,v)

The final result :

{
    "duration": 1942,
    "frame_id": 0,
    "detect1": [
        {
            "type": {
                "s_type1": [
                    {
                        "confidence": 98.70016,
                        "name": "c*****"
                    },
                ],
                "s_type2": [
                    {
                        "confidence": 92.82484,
                        "name": "b*****"
                    }
                ],
                "Box": [
                    80.80994,
                    170.0965,
                    1091.778
                ]
            },
            "confidences": [
                90.08681,
                99.91595,
                90.12489

            ]
        }
    ]
}

I was trying to do it with the JMESPath library but I can't get a good result.

Have someone any idea to do this ?

Thanks

Ib D
  • 391
  • 1
  • 5
  • 17
  • how could define which value you should keep and which one have to move? – Sabil Sep 16 '21 at 21:42
  • the initial result is an output of an ALPR process, and the final result that I will want to keep is arbitrary and depends on what I want to do with it in another python program – Ib D Sep 16 '21 at 21:50

1 Answers1

1

Using jmespath to get your desired output:

import jmespath
expression = """{duration: duration, 
                 frame_id: frame_id, 
                 detect1: [{type:{s_type1: [detect1[].type.s_type1[].merge({confidence: confidence, name: name})|[0]],                                                               
                                  s_type2: [detect1[].type.s_type2[].merge({confidence: confidence, name: name})|[0]],
                                  Box: detect1[].type.Box[]},
                            confidences: detect1[].confidences[]  
                           }
                           ]}
             """

expression = jmespath.compile(expression)

expression.search(json)

{'duration': 1942,
 'frame_id': 0,
 'detect1': [{'type': {'s_type1': [{'confidence': 98.70016, 'name': 'c*****'}],
    's_type2': [{'confidence': 92.82484, 'name': 'b*****'}],
    'Box': [80.80994, 170.0965, 1091.778]},
   'confidences': [90.08681, 99.91595, 90.12489]}]}

 
sammywemmy
  • 27,093
  • 4
  • 17
  • 31