Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Unable to get the data
#1
Hi All,

I am trying to get one of the value named as "TotalNumberOfProducts" from one of the API.The API is below
http://xx:8080/maintenance/job?jobType=IMPORT&resultLimit=30
i was able to get other parameters like recordsprocesed,jobid etc and tried the below code to get the required data
#totalProducts=responseurl['statistics'].get('totalProducts')[0]
#totalProducts=responseurl[1]

both the ways are not giving any result Could you please assist as am new to this language.

responseurl = requests.get('http://xx:8080/maintenance/job?jobType=IMPORT&resultLimit=30')
                if(responseurl.ok):
                        jData = json.loads(responseurl.content)
                        #print jData
                        if jData > 0:
                                for responseurl in jData['response']:
                                        starttime=responseurl['statistics']['startTime']
                                        jobId= responseurl['jobId']
                                        status = responseurl['status']
                                        catalogId = responseurl['catalogId']
                                        recordsProcessed=responseurl['statistics']['recordsProcessed']
                                        recordsFailed=responseurl['statistics']['recordsFailed']
                                        fileName=responseurl['fileName']
                                        duration=responseurl['statistics']['duration']
                                        throughput=responseurl['statistics']['throughput']
                                        
API output

{u'response': [{u'status': u'COMPLETE', u'statistics': {u'estimatedTimeToFinish': u'0 seconds', u'workExpected': 1613, u'recordsFailed': 0, u'throughput': 322, u'percentComplete': 100, u'startTime': u'2019-05-22T11:55:01.642Z', u'duration': u'5 seconds', u'recordsProcessed': 1613, u'endTime': u'2019-05-22T11:55:07.126Z'}, u'expirationDatetime': u'2019-05-25T11:55:01.639Z', u'completedSteps': [{u'statistics': {u'workExpected': 1, u'recordsFailed': 0, u'throughput': 0, u'percentComplete': 100, u'startTime': u'2019-05-22T11:55:01.642Z', u'duration': u'0 seconds', u'recordsProcessed': 1, u'endTime': u'2019-05-22T11:55:01.653Z'}, u'workExpected': 1, u'catalogId': u'xx', u'header': {u'language': u'xx', u'totalProducts': 1332, u'country': u'xx', u'catalogId': u'xx', u'currency': u'xx', u'version': u'xx', u'partnerCode': u'xx'}, u'concreteType': u'CreateCatalogStep', u'workType': u'CREATE'}, {u'statistics': {u'workExpected': 256, u'recordsFailed': 0, u'throughput': 0, u'percentComplete': 100, u'startTime': u'2019-05-22T11:55:01.653Z', u'duration': u'0 seconds', u'recordsProcessed': 256, u'endTime': u'2019-05-22T11:55:01.757Z'}, u'workExpected': 256, u'fileName': u'/xx', u'catalogId': u'xx', u'concreteType': u'CategoryImportFromFileStep', u'workType': u'CATEGORY_IMPORT'}, {u'workExpected': 1, u'statistics': {u'workExpected': 1, u'recordsFailed': 0, u'throughput': 0, u'percentComplete': 100, u'startTime': u'2019-05-22T11:55:01.757Z', u'duration': u'0 seconds', u'recordsProcessed': 1, u'endTime': u'2019-05-22T11:55:01.781Z'}, u'concreteType': u'EvictCacheStep', u'workType': u'EVICT_CACHE', u'catalogId': u'xx'}, {u'statistics': {u'estimatedTimeToFinish': u'0 seconds', u'workExpected': 1332, u'recordsFailed': 0, u'throughput': 266, u'percentComplete': 100, u'startTime': u'2019-05-22T11:55:01.781Z', u'duration': u'5 seconds', u'recordsProcessed': 1332, u'endTime': u'2019-05-22T11:55:07.023Z'}, u'workExpected': 1332, u'bsinsRequiringSupplierSelection': [], u'catalogId': u'xx', u'startFrom': 0, u'merchantsProcessed': [u'xx'], u'fileName': u'xx', u'concreteType': u'ImportProductsFromFileStep', u'workType': u'IMPORT'}, {u'merchants': [u'xx'], u'statistics': {u'workExpected': 23, u'recordsFailed': 0, u'throughput': 0, u'percentComplete': 100, u'startTime': u'2019-05-22T11:55:07.023Z', u'duration': u'0 seconds', u'recordsProcessed': 23, u'endTime': u'2019-05-22T11:55:07.117Z'}, u'workExpected': 23, u'lastImportDateTime': u'2019-05-22T11:55:01.781Z', u'catalogId': u'xx', u'bsinsRequiringSupplierSelection': [], u'concreteType': u'ExpireMerchantProductsStep', u'workType': u'EXPIRE'}, {u'statistics': {u'workExpected': 0, u'recordsFailed': 0, u'throughput': 0, u'percentComplete': 0, u'startTime': u'2019-05-22T11:55:07.117Z', u'duration': u'0 seconds', u'recordsProcessed': 0, u'endTime': u'2019-05-22T11:55:07.125Z'}, u'workExpected': 0, u'catalogId': u'xx', u'concreteType': u'SelectPreferredSupplierStep', u'multiSupplierProducts': [], u'workType': u'SELECT_SUPPLIER'}], u'jobId': 7620, u'startFrom': 0, u'pendingSteps': [], u'fileName': u'/xx', u'catalogId': u'xx', u'jobType': u'IMPORT'},
Reply
#2
If you look at the structure of that output, 'totalProducts' is in responseurl['completedSteps'][0]['header']['totalProducts'] (but not in any of the other items in that list).
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
So I copied the API output you posted into Notepad++, installed JSTool plugin to use "Plugin -> JSTool -> JSFormat" to nicely format the output, and it can be clearly seen that the json output is incomplete, and that's the reason why it doesn't load.
Error:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
Not sure if you posted incomplete output or the response was incomplete itself but it's kinda hard to practically help using it...
Reply
#4
use this code
response = requests.get('http://xx:8080/maintenance/job?jobType=IMPORT&resultLimit=30')
if resposnse.ok:
    data = response.json()
    with open('data.json', 'w') as f:
        json.dump(data, f, indent=4)
to save the json response to file, nicely formatted
e.g. without u in front of the keys
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
the solution given by ichabod801 has worked.thanks
responseurl['completedSteps'][0]['header']['totalProducts']
Reply
#6
(May-22-2019, 02:35 PM)pythonFresher Wrote: the solution given by ichabod801 has worked.thanks
it's unclear if totalProducts is present only in the first element or there may be others (definitely not all). You need to know better if famillair with the data.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Unable to request image from FORM Data usman 0 990 Aug-18-2022, 06:23 PM
Last Post: usman
  Unable to Validate csv blanck data and write in csv prashant18 0 1,529 Jul-25-2020, 12:08 PM
Last Post: prashant18
  Unable to do the proper split using re.sub incase of missing data. Karz 1 1,854 Nov-17-2019, 05:58 PM
Last Post: buran
  unable to indent json data dataplumber 4 3,000 Oct-22-2019, 01:55 PM
Last Post: dataplumber
  Unable to read data from string base64 representing a .xlsx file max 2 15,303 Apr-05-2018, 07:58 PM
Last Post: max

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020