0

Pretty new to Python API calls I Have the JSON data sample below

I can get data calling for example (swell.*) or (swell.minBreakingHeight) and it returns all the swell data no worries. So ok with a working request

I can't seem to narrow it down with success example swell.primary.height Obviously the format above here is incorrect and keeps returning []

How do I get in that extra level?

[{
timestamp: 1366902000,
localTimestamp: 1366902000,
issueTimestamp: 1366848000,
fadedRating: 0,
solidRating: 0,
swell: {
    minBreakingHeight: 1,
    absMinBreakingHeight: 1.06,
    maxBreakingHeight: 2,
    absMaxBreakingHeight: 1.66,
    unit: "ft",
    components: {
         combined: {
         height: 1.1,
         period: 14,
         direction: 93.25,
         compassDirection: "W"
    },
    primary: {
         height: 1,
         period: 7,
         direction: 83.37,
         compassDirection: "W"
    },
K DawG
  • 13,287
  • 9
  • 35
  • 66

1 Answers1

0

Working with your data snippet:

data = [{
'timestamp': 1366902000,
'localTimestamp': 1366902000,
'issueTimestamp': 1366848000,
'fadedRating': 0,
'solidRating': 0,
'swell': {
    'minBreakingHeight': 1,
    'absMinBreakingHeight': 1.06,
    'maxBreakingHeight': 2,
    'absMaxBreakingHeight': 1.66,
    'unit': "ft",
    'components': {
         'combined': {
         'height': 1.1,
         'period': 14,
         'direction': 93.25,
         'compassDirection': "W"
    },
    'primary': {
         'height': 1,
         'period': 7,
         'direction': 83.37,
         'compassDirection': "W"
    }
}
}
}
]

In [54]: data[0]['timestamp']
Out[54]: 1366902000

In [55]: data[0]['swell']['components']['primary']['height']
Out[55]: 1

So using your dot notation, you should be calling:

swell.components.primary.height

For furhter insight on parsing json files refer to this other stackoverflow question

Community
  • 1
  • 1
mattgathu
  • 1,129
  • 1
  • 19
  • 28