Home > Blockchain >  "Failed to convert value of type 'java.lang.String' to required 'java.util.List&
"Failed to convert value of type 'java.lang.String' to required 'java.util.List&

Time:04-16

This one has the better of me, for sure.

I am attempting to use the USDA FoodData Central Rest API for Python, and I have extensively reviewed the related documentation, online materials and SwaggerHub simulations.

To make a long story short, it seems I need to convert a Python list to a 'java.util.List', something not in the USDA documentation, and something I have not been able to make happen. Despite my efforts, I am unable to even attempt to make that conversion happen, because I cannot install the Python dependencies. It boils down to two questions overall:

  1. How do I convert a Python list to a Java.Util.List if possible?
  2. What can I do about the problems installing the dependencies (e.g. jep) when it appears they are deprecated?

The USDA site sends you to a SwaggerHub simulation. It was helpful, and it gave me the URL pasted below as a model for my API. However, the instructions regarding the required 'fdcIds list' says nothing at all about the need to convert to Java.

To be clear, the 'fdcIds list' is simply the desired Python list of the six-digit numbers that represent all the food items and products in the USDA database. You can request up to 20 at a time and the API returns a complete nutritional profile of each. You will see what I mean if you follow the SwaggerHub link below

Here is the Swaggerhub model URL with the link below:

https://api.nal.usda.gov/fdc/v1/foods?fdcIds=173567&fdcIds=173565&fdcIds=173571&format=abridged&nutrients=208&nutrients=204&nutrients=205&api_key=xxxxxxxxxxxxxxxx

https://app.swaggerhub.com/apis/fdcnal/food-data_central_api/1.0.0#/info 

Here are SwaggerHub instructions for the fdcID list pasted in their entirety:

"fdcIds (reguired)
array[string]
(query)
List of multiple FDC ID's. Should be comma separated list (e.g. fdcIds=534358,373052) or repeating parameters (e.g. fdcIds=534358&fdcIds=373052)."

I think the rest of my code (below) is fine, because otherwise the SwaggerHub simulation produces the desired results.

Here is the complete error message I received afterwards:


'error': 'Bad Request',
 'message': "Failed to convert value of type 'java.lang.String' to required "
            "type 'java.util.List'; nested exception is "
            'java.lang.NumberFormatException: For input string: '
            '"[{\'fdcids\':173567},{\'fdcids\':173565},{\'fdcids\':173571},{\'fdcids\':173569},{\'fdcids\':173564}]"',
 'path': '/portal-data/api/v1/foods',
 'status': 400,
 'timestamp': '2022-04-15T21:49:49.609 0000'}

I know Python and JavaScript. I know nothing about Java.

However, I attempted to install the Python packages necessary for this operation, such as jdk and jep, but I ran into additional error messages, as it seems the packages are deprecated(?) - though I am not sure. I forced the jep installation, but it still did not install successfully.

The error messages related to the failed installations are too long to paste here; but I will provide examples that seem to represent the gist of the problem.

error: subprocess-exited-with-error
error: metadata-generation-failed
note: This error originates from a subprocess, and is likely not a problem with pip.

I have spent the past several trying to make this API work. I've been working on the related app for almost a year. I will send a request for technical assistance to the USDA, but I could use some help if anyone has some genuine insights or has resolved similar issues.

The USDA API is a popular one. It apparently works for most developers. I am hoping someone can help. I am a bit mystified by these obstacles.

My operating system is Windows 10 64 bit.

My code, as it stands, is pasted below.

I should note the variables in the URL were originally in a "parameters" dictionary. But then, I started to receive error messages stating the dictionary did not contain the fields in the requested URL, which was not true. It seemed to work better when I made them standalone variables...except for the conversion issues related to the fdcIds list.

At that point, I started to receive error messages like the one in the title related to my failure to convert the Python list to a Java Util.List. I have reviewed all the online information and videos I could find about Python rest apis and possible solutions to the Java conversion problem, including all related questions on Stackoverflow.

I am at a loss as to how I should proceed.

Your sincere feedback is much appreciated.

Thank you for reviewing my question.

Below, I pasted my code in its entirety. Again, I have also tried to place the variables in the URL inside a parameters dictionary. But it only caused more trouble.

import requests
import pprint

api_key = 'AcAm9H2jcFikw4f5PmJl0hFJSjreMwg9BN621tQD'               
pagesize = 5,
fdcIds = [{'fdcids': 173567}, {'fdcids': 173565}, {'fdcids': 173571}, {'fdcids': 173569}, {'fdcids': 173564}]
format = 'abridged'
dataType = 'SR Legacy'
foodNutrients = [208, 204,205]

url = f"https://api.nal.usda.gov/fdc/v1/foods?fdcIds={fdcIds}&format={format}&dataType={dataType}&nutrients={foodNutrients}&pagesize={pagesize}'&api_key={api_key}"

headers = {'Content-Type': 'application/json;charset=utf-8'}

data = requests.get(url, headers).json()
print(requests.status_codes)
pprint.pprint(data)

CodePudding user response:

Change fdcIds as shown below and also foodNutrients:

import requests
import pprint

api_key = 'AcAm9H2jcFikw4f5PmJl0hFJSjreMwg9BN621tQD'          
pagesize = 5
# fdcIds = [{'fdcids': 173567}, {'fdcids': 173565}, {'fdcids': 173571}, {'fdcids': 173569}, {'fdcids': 173564}]
fdcIds = "173567,173565,173571,173569,173564"
f = 'abridged'
dataType = 'SR Legacy'
foodNutrients = "208, 204, 205"

url = f"https://api.nal.usda.gov/fdc/v1/foods?fdcIds={fdcIds}&format={f}&dataType={dataType}&nutrients={foodNutrients}&pagesize={pagesize}'&api_key={api_key}"
print(url)
headers = {'Content-Type': 'application/json;charset=utf-8'}

data = requests.get(url, headers).json()
print(requests.status_codes)
pprint.pprint(data)

Result:

[{'dataType': 'SR Legacy',
  'description': 'Shortening frying (heavy duty), beef tallow and cottonseed',
  'fdcId': 173567,
  'foodNutrients': [{'amount': 100,
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'},
                    {'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'},
                    {'amount': 900,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'}],
  'ndbNumber': '4550',
  'publicationDate': '2019-04-01'},
 {'dataType': 'SR Legacy',
  'description': 'Fat, chicken',
  'fdcId': 173564,
  'foodNutrients': [{'amount': 900,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'},
                    {'amount': 99.8,
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'},
                    {'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'}],
  'ndbNumber': '4542',
  'publicationDate': '2019-04-01'},
 {'dataType': 'SR Legacy',
  'description': 'Fat, turkey',
  'fdcId': 173571,
  'foodNutrients': [{'amount': 99.8,
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'},
                    {'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'},
                    {'amount': 900,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'}],
  'ndbNumber': '4575',
  'publicationDate': '2019-04-01'},
 {'dataType': 'SR Legacy',
  'description': 'Oil, soybean, salad or cooking, (partially hydrogenated) and '
                 'cottonseed',
  'fdcId': 173565,
  'foodNutrients': [{'amount': 100,
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'},
                    {'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'},
                    {'amount': 884,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'}],
  'ndbNumber': '4543',
  'publicationDate': '2019-04-01'},
 {'dataType': 'SR Legacy',
  'description': 'Shortening industrial, soybean (hydrogenated) and cottonseed',
  'fdcId': 173569,
  'foodNutrients': [{'amount': 0.0,
                     'name': 'Carbohydrate, by difference',
                     'number': '205',
                     'unitName': 'G'},
                    {'amount': 884,
                     'derivationCode': 'NC',
                     'derivationDescription': 'Calculated',
                     'name': 'Energy',
                     'number': '208',
                     'unitName': 'KCAL'},
                    {'amount': 100,
                     'derivationCode': 'A',
                     'derivationDescription': 'Analytical',
                     'name': 'Total lipid (fat)',
                     'number': '204',
                     'unitName': 'G'}],
  'ndbNumber': '4554',
  'publicationDate': '2019-04-01'}]

CodePudding user response:

There are two ways in which you can pass the fdcIds:

Should be comma separated list (e.g. fdcIds=534358,373052) or repeating parameters (e.g. fdcIds=534358&fdcIds=373052).

This is mentioned here. Try to pass the ids in these formats.

  • Related