0

I'm having trouble with a JSON object being passed to me by one of our products API's. I'm using Python 2.7 to create a function to let our customer service team see details about jobs that are posted on our website. The JSON package returns an array of objects that each contain an array and an object. I need to read the array associated with one of the objects inside the main object, however their not nested. As in the array of applicants is not nested inside the object of Job. This means my usual "response[0][0]['applicantName']" won't work here.

The data below is updated, to represent what the API is actually giving me. My apologies before, I had edited it in order to protect the data. Still done the same, but it's the actual result.

What I'd like to do is let a user input the jobId and I'll provide them with a list of all the applicants related to that jobID. Since the jobID can sometimes be non-sequential, I can't use an index number, it must be the jobID number.

Can someone help?

Heres the JSON structure I get:

[{u'bids': [{u'applicantId': 221,
            u'comment': 'I have applied to the job'},
            {u'applicantId': 221,
            u'comment': 'I have applied to the job'}],
  u'job': {u'jobId': 1}},
 {u'bids': [{u'applicantId': 221,
              u'comment': 'I have applied to the job'},
              {u'applicantId': 221,
              u'comment': 'I have applied to the job'}],
  u'job': {u'jobId': 1}}]

As I said, I'm working in python 2.7 using the "requests" library to call the API and .json() to read it.

Thanks in advance!

beerandsmiles
  • 134
  • 3
  • 12
  • If your data _really_ looks like that then the API producing it is broken. If not, please edit your data sample so we can tell you how to process it without having to guess its real structure. – PM 2Ring Aug 15 '16 at 17:48
  • This is similar to the looks-like-JSON-but-isnt-JSON format in this question: http://stackoverflow.com/questions/20691134/pyparsing-parsing-semi-json-nested-plaintext-data-to-a-list, including some sample parser code. – PaulMcG Aug 16 '16 at 00:04

1 Answers1

1

That content doesn't seem to be a valid json, which means you won't be able to parse it with the typical well-known json.loads function.

Also, you won't be able to use ast.literal_eval cos it's not a valid python expression.

Not sure this will be a good idea... but assuming you're getting that content as a string I'd try to write my own parser for that type of server-objects instead or just looking for an external library able to parse them.

BPL
  • 9,632
  • 9
  • 59
  • 117