Search code examples
pythonjsonjmespath

Copy some k,v from existing JSON to constitute another JSON in Python


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


Solution

  • 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]}]}