0

I'm writing a name matching api for a client and I'm using flask's jsonify functionality for the first time. Here's what I need to do:

A user enters a company name(s) into the front end interface calling the api which then performs a search based on a set of fuzzy matching algorithm's return company information for all matches in json format.

Programmatically building the json string results in the following sample json output - could be more companies returned based on input strings

{
    "results": {
        "value": "Company One",
        "matches": [
            {
                "matchedNameId": "2298685",
                "matchedName": "Company Inc",
                "certainty": "1.0",
                "duns": "xxxxx",
                "parentDuns": "yyyyyyy",
                "annualRevenue": "$$$$$$$$",
                "country": "United States",
                "crsCode": "4",
                "location": "United States",
                "naics": "aaaaa",
                "assigned_status": "False"
            }
            {
                "annual_revenue": "$$$$$$$",
                "certainty": "1.0",
                "country": "New Zealand",
                "crs_code": "24",
                "duns": "XXXXXX",
                "location": "",
                "match_name": "COMPANY LIMITED",
                "match_name_id": "6266334",
                "naics": "nan",
                "parent_duns": ""
            }
        ],
        "metrics": {
            "Total time for run (Total Seconds)": "0.156943",
            "Average Query Time (seconds)": "0.068599",
            "Total Rows Processed": "9",
            "Highest Row Count": "9",
            "Average Row Count": "9.0",
            "Highest matching index": "8",
            "Average index": "18.0",
            "Total Num Errors": "0",
            "Total Strings To Match": "1",
            "Total Matches": "2"
        }
    }
}

Trying to use the jsonify functionality is giving me the following results based on the approach I take--

If I merge 2 lists (company name, matched names(nested json inside Company) using the .exend(object) to create one object and then call return jsonify(data=[e.serialize() for e in agg_set_dict]) I get the following json

{
"data": [
    {
        "company name": "Farmers Group Inc."
    },
    {
        "annual_revenue": "$1,913,000.00",
        "certainty": "1.0",
        "country": "New Zealand",
        "crs_code": "24",
        "duns": "593468119",
        "location": "",
        "match_name": "AG FARMERS LIMITED",
        "match_name_id": "6266334",
        "naics": "nan",
        "parent_duns": ""
    },
    {
        "annual_revenue": "$15,849,518.00",
        "certainty": "1.0",
        "country": "United States",
        "crs_code": "4",
        "duns": "35549526",
        "location": "United States",
        "match_name": "Farmers Association Inc",
        "match_name_id": "9418",
        "naics": "4539.0",
        "parent_duns": "35549526"
    },
    {
        "highest matching index": 8,
        "highest row count": 9,
        "total client time": 0.103005,
        "total index": 36,
        "total number errors": 0,
        "total number matches": 18,
        "total query time": 0.228998,
        "total rows": 9,
        "total strings": 1
    }
]

}

The second approach I tried was to loop through the two lists adding the elements of the second list to the first list and then calling return jsonify(agg_set_object.serialize(agg_set_dict)) which gave me the following json output:

{
"annual_revenue": "$1,913,000.00",
"certainty": "1.0",
"company name": "Farmers Group Inc.",
"country": "New Zealand",
"crs_code": "24",
"duns": "593468119",
"location": "",
"match_name": "AG FARMERS LIMITED",
"match_name_id": "6266334",
"naics": "nan",
"parent_duns": ""

}

Neither one of these approaches gives me what I want (sample json 1) so my question / issue is how do I using jsonify get the desired output?

Thanks, Bill

B. Youngman
  • 123
  • 1
  • 12
  • This question needs more python code to be reproducable. What does `agg_set_dict` actually look like? This seems like an issue of building the correct python datastructure to pass to `jsonify`, rather than an issue with jsonify itself. – v25 Jul 01 '22 at 17:56
  • Here is the call to agg_set_dict: 'agg_set_dict = agg_set_object.merge_dicts(original_name_dict, matched_names_dict)' ' def merge_dicts(self, original_name_dict, matched_names_dict): for elements in matched_names_dict: original_name_dict.append(elements) return original_name_dict' The original_name_dict contains company name(s) and the matched_name_dict contains detailed information for a specific company. I hope this helps. – B. Youngman Jul 01 '22 at 18:22

0 Answers0