Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Python help

  • 09-03-2018 09:17PM
    #1
    Registered Users, Registered Users 2 Posts: 1,369 ✭✭✭


    Hello,

    I'm struggling with handling a JSON response from an app that I'm playing about with it.

    I have written a little app that pulls back a profile from Companies House in the uk.

    For this question, I'm using a google company and I get this response:

    {
    "company_number": "10197747",
    "company_name": "GOOGLE ADWORD EUROPE SERVICES LTD",
    "has_charges": false,
    "company_status": "active",
    "undeliverable_registered_office_address": false,
    "has_insolvency_history": false,
    "etag": "eb63a58d61840d804e7adaaddc179fe87ee29d1b",
    "jurisdiction": "england-wales",
    "registered_office_address": {
    "address_line_2": "Suite 33854 Advantage Business Centre",
    "address_line_1": "132-134 Great Ancoats Street",
    "locality": "Manchester",
    "postal_code": "M4 6DE",
    "country": "England"
    },
    "type": "ltd",
    "accounts": {
    "next_made_up_to": "2018-05-31",
    "overdue": false,
    "next_due": "2019-02-28",
    "last_accounts": {
    "type": "dormant",
    "period_start_on": "2016-05-25",
    "made_up_to": "2017-05-31",
    "period_end_on": "2017-05-31"
    },
    "accounting_reference_date": {
    "month": "05",
    "day": "31"
    },
    "next_accounts": {
    "period_start_on": "2017-06-01",
    "due_on": "2019-02-28",
    "overdue": false,
    "period_end_on": "2018-05-31"
    }
    },
    "date_of_creation": "2016-05-25",
    "confirmation_statement": {
    "overdue": false,
    "next_due": "2018-04-27",
    "next_made_up_to": "2018-04-13",
    "last_made_up_to": "2017-04-13"
    },
    "links": {
    "self": "/company/10197747",
    "filing_history": "/company/10197747/filing-history",
    "officers": "/company/10197747/officers",
    "persons_with_significant_control": "/company/10197747/persons-with-significant-control"
    },
    "sic_codes": [
    "82990"
    ],
    "registered_office_is_in_dispute": false,
    "can_file": true
    }

    I get this via Python with:


    r = requests.get('https://api.companieshouse.gov.uk/company/10197747', auth=HTTPBasicAuth('secret_key', ''))
    company_data = json.loads(r.text)

    So I have a data structiure called company_data

    However, I want to start pulling individual elements that are a 'nested dict' (I don't know the real term.

    I can query top level strings (such as "type" or "sic_codes") but I can't get "officers" which are nested under "links".

    Can someone help?


Comments

  • Moderators, Computer Games Moderators Posts: 4,282 Mod ✭✭✭✭deconduo


    ezra_ wrote: »
    Hello,

    I'm struggling with handling a JSON response from an app that I'm playing about with it.

    I have written a little app that pulls back a profile from Companies House in the uk.

    For this question, I'm using a google company and I get this response:

    {
    "company_number": "10197747",
    "company_name": "GOOGLE ADWORD EUROPE SERVICES LTD",
    "has_charges": false,
    "company_status": "active",
    "undeliverable_registered_office_address": false,
    "has_insolvency_history": false,
    "etag": "eb63a58d61840d804e7adaaddc179fe87ee29d1b",
    "jurisdiction": "england-wales",
    "registered_office_address": {
    "address_line_2": "Suite 33854 Advantage Business Centre",
    "address_line_1": "132-134 Great Ancoats Street",
    "locality": "Manchester",
    "postal_code": "M4 6DE",
    "country": "England"
    },
    "type": "ltd",
    "accounts": {
    "next_made_up_to": "2018-05-31",
    "overdue": false,
    "next_due": "2019-02-28",
    "last_accounts": {
    "type": "dormant",
    "period_start_on": "2016-05-25",
    "made_up_to": "2017-05-31",
    "period_end_on": "2017-05-31"
    },
    "accounting_reference_date": {
    "month": "05",
    "day": "31"
    },
    "next_accounts": {
    "period_start_on": "2017-06-01",
    "due_on": "2019-02-28",
    "overdue": false,
    "period_end_on": "2018-05-31"
    }
    },
    "date_of_creation": "2016-05-25",
    "confirmation_statement": {
    "overdue": false,
    "next_due": "2018-04-27",
    "next_made_up_to": "2018-04-13",
    "last_made_up_to": "2017-04-13"
    },
    "links": {
    "self": "/company/10197747",
    "filing_history": "/company/10197747/filing-history",
    "officers": "/company/10197747/officers",
    "persons_with_significant_control": "/company/10197747/persons-with-significant-control"
    },
    "sic_codes": [
    "82990"
    ],
    "registered_office_is_in_dispute": false,
    "can_file": true
    }

    I get this via Python with:


    r = requests.get('https://api.companieshouse.gov.uk/company/10197747', auth=HTTPBasicAuth('secret_key', ''))
    company_data = json.loads(r.text)

    So I have a data structiure called company_data

    However, I want to start pulling individual elements that are a 'nested dict' (I don't know the real term.

    I can query top level strings (such as "type" or "sic_codes") but I can't get "officers" which are nested under "links".

    Can someone help?

    You probably want to do something like:
    print(company_data["links"]["officers"])
    


  • Registered Users, Registered Users 2 Posts: 1,369 ✭✭✭ezra_


    deconduo wrote: »
    You probably want to do something like:
    print(company_data["links"]["officers"])
    

    Very simple! I had my syntax wrong, thank you


Advertisement