Can I remove double quotes from keys in this json but not the values?

I have the following json string:

'{"start_time": {"_gte": "2021-10-10 19:00:00"}, "end_time": {"_gte": "2021-10-10 19:30:00"}}'

and I need to get:

'{start_time: {_gte: "2021-10-10 19:00:00"}, end_time: {_gte: "2021-10-10 19:30:00"}}'

Is this possible?

1 answer

  • answered 2021-10-12 16:21 rv.kvetch

    I guess something like this should work:

    import json
    from typing import List, Tuple, Any
    
    
    string = '{"start_time": {"_gte": "2021-10-10 19:00:00"}, "end_time": {"_gte": "2021-10-10 19:30:00"}}'
    
    
    class MyDict(dict):
        def __repr__(self):
            return '{' + ', '.join([f'{k}: {v!r}' for k, v in self.items()]) + '}'
    
    
    def hook_fn(value: List[Tuple[str, Any]]):
        return MyDict(value)
    
    
    print(json.loads(string, object_pairs_hook=hook_fn))
    

    it prints:

    {start_time: {_gte: '2021-10-10 19:00:00'}, end_time: {_gte: '2021-10-10 19:30:00'}}
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum