2

How do I exclude records based on the presence of a certain field (like the equivalent of SQL's [fieldname] Is Null)?

Example:

  • Results for Closed questions include field closed_date=1234567890.

  • Results for Open questions do not include this field as a Null as I would expect.

Is there a parameter I can add to the URL to exclude questions that have the field?


The only information I could find was from the documentation:

Absent Fields

To save space, the API does not return null values, instead opting to exclude the field altogether in the returned JSON. Fields that are not always guaranteed to be set (closed_date on question objects for example) are indicated in the per-type documentation. (Source)

1 Answer 1

1

You have 2 questions:

  1. "How do I exclude records based on the presence of a certain field?"
    Answer: The API will not do this for you; your code must filter out these results.

  2. "(How do I ) return only open questions?"
    Answer: Use the /search/advanced route.


Exclude records where field is present:

The API does not do this for any field, and seems unlikely to add the capability. (See this somewhat related answer to "API responses are sometimes missing fields".}

Your app/script must loop through the results and exclude the ones you are not interested in.

For example, this query to /questions:
          /2.2/questions?tagged=notifications&site=stackapps

returns:

{ "items": [ {
      "title": "Top Bar for Chat Rooms"
    }, {
      "title": "Get our Stack Exchange inbox as native notification on Chrome"
    ...
    }, {
      "closed_date": 1382175473,
      "title": "Mark notification/inbox item as read"
    }, {
      "title": "Trying to get the Total results count crashes the /notifications path"
    ...
    } ],
  "total": 13
}

Your code would loop through that and reject items where typeof closed_date != "undefined.



Return only open questions:

For such "complex" queries, the /search/advanced route was added.

There you have two options:

  1. You can use the closed=False parameter provided:
    /2.2/search/advanced?closed=False&tagged=notifications&site=stackapps

  2. Or you can leverage the q parameter -- which mimics much of the functionality of the website search:
    /2.2/search/advanced?q=closed:no&tagged=notifications&site=stackapps

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .