Get rid of Mongo $ signs in JSON

前端 未结 2 1885
鱼传尺愫
鱼传尺愫 2021-01-21 00:36

I am building python backend for SPA (Angular) using MongoDB.

Here is what I use: Python 3.4, MongoDB 3, Flask, flask-mongoe

相关标签:
2条回答
  • 2021-01-21 00:56

    If you are confident you want to get rid of all the similar cases, then you can certainly write code that matches that pattern. For example:

    info = [
        {
            "_id": {
                "$oid": "55c737029380f82fbf52eec3"
            },
            "created_at": {
                "$date": 1439129906376
            },
            "desc": "Description.....",
            "title": "This is title"
        },
        #etc...
    ]
    
    def fix_array(info):
        ''' Change out dict items in the following case:
               - dict value is another dict
               - the sub-dictionary only has one entry
               - the key in the subdictionary starts with '$'
            In this specific case, one level of indirection
            is removed, and the dict value is replaced with
            the sub-dict value.
        '''
        for item in info:
            for key, value in item.items():
                if not isinstance(value, dict) or len(value) != 1:
                    continue
                (subkey, subvalue), = value.items()
                if not subkey.startswith('$'):
                    continue
                item[key] = subvalue
    
    fix_array(info)
    print(info)
    

    This will return this:

    [{'title': 'This is title', 'created_at': 1439129906376, 'desc': 'Description.....', '_id': '55c737029380f82fbf52eec3'}]
    

    Obviously, reformatting that with JSON is trivial.

    0 讨论(0)
  • 2021-01-21 01:11

    I found a neat solution to my problem in flask-restful extension which I use.

    It provides fields module.

    Flask-RESTful provides an easy way to control what data you actually render in your response. With the fields module, you can use whatever objects (ORM models/custom classes/etc.) you want in your resource. fields also lets you format and filter the response so you don’t have to worry about exposing internal data structures.

    It’s also very clear when looking at your code what data will be rendered and how it will be formatted.

    Example:

    from flask_restful import Resource, fields, marshal_with
    
    resource_fields = {
        'name': fields.String,
        'address': fields.String,
        'date_updated': fields.DateTime(dt_format='rfc822'),
    }
    
    class Todo(Resource):
        @marshal_with(resource_fields, envelope='resource')
        def get(self, **kwargs):
            return db_get_todo()  # Some function that queries the db
    

    Flask-RESTful Output Fields Documentation

    0 讨论(0)
提交回复
热议问题