Parsing nested JSON data

后端 未结 5 2025
执笔经年
执笔经年 2020-12-18 05:57

This JSON output is from a MongoDB aggregate query. I essentially need to parse the nested data JSON down to the following to the \'total\' and \'_id\'

相关标签:
5条回答
  • 2020-12-18 06:10

    This should do.

    import json
    
    def parse_json(your_json):
        to_dict = json.loads(your_json)
        for item in to_dict['results']:
            print item['total']
    
    0 讨论(0)
  • 2020-12-18 06:18

    The response you are getting from the mongodb seems to be the compatible to put for the dictionary type object. as

    {
        'ok': 1.0,  'result': [
            {
                'total': 142250.0, 
                '_id': 'BC'
            }, 
            {
                'total': 210.88999999999996,
                 '_id': 'USD'
            }, 
            {
                'total': 1065600.0, 
                '_id': 'TK'
            }
        ]
    }
    

    Instead of putting it into multiline string and replacing single quotes in double quotes, can't we directly assign it to the dict type object. and perform further operation on it like:

    json_data = {
        'ok': 1.0,
        'result':
            [
                {
                    'total': 142250.0,
                    '_id': 'BC'
                },
                {
                    'total': 210.88999999999996,
                    '_id': 'USD'
                },
                {
                    'total': 1065600.0,
                    '_id': 'TK'
                }
        ]
    }
    

    And:

    for data in json_data['result']:
        print(data['total'], data['_id'])
    
    0 讨论(0)
  • 2020-12-18 06:31

    NOTE: Your JSON response from MongoDB is not actually valid. JSON requires double-quotes ("), not single-quotes (').

    I'm not sure why your response has single-quotes instead of double-quotes but from the looks of it you can replace them and then just use the built-in json module:

    from __future__ import print_function
    import json
    
    response = """{
        'ok': 1.0, 
        'result': [
            {
                'total': 142250.0, 
                '_id': 'BC'
            }, 
            {
                'total': 210.88999999999996,
                 '_id': 'USD'
            }, 
    
            {
                'total': 1065600.0, 
                '_id': 'TK'
            }
            ]
    }"""
    
    # JSON requires double-quotes, not single-quotes.
    response = response.replace("'", '"')
    response = json.loads(response)
    for doc in response['result']:
        print(doc['_id'], doc['total'])
    
    0 讨论(0)
  • 2020-12-18 06:34
    import json
    
    data = json.loads(mongo_db_json)
    result = data['result']
    for value_dict in result:
        print '{0}, {1}'.format(value['total'], value['_id'])
    

    This should work

    0 讨论(0)
  • 2020-12-18 06:35

    Your example text is not valid JSON text. JSON string must start with a " quotation mark, not '; but it seems a valid Python literal that you can parse with ast.literal_eval() function:

    import ast
    
    data = ast.literal_eval(input_string)
    for item in data["result"]:
        print("{total}, {_id}".format(**item))
    

    Output

    142250.0, BC
    210.89, USD
    1065600.0, TK
    

    A better way might be to fix the querying process to get valid JSON and use json module to parse it.

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