Converting JSON encoded in HTML to JSON using BeautifulSoup - html
I know similar questions have been asked here, but I'm still struggling to find a solution here. I'm able to parse raw HTML from the bandsintown website, using beautifulSoup, but my ultimate goal is to access the script on the page and access a JSON embedded in the script. Opening the page source, I can see that "eventsJsonLd" is what I need:
"jsonLdContainer":{"eventsJsonLd":[{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-25","endDate":"2019-01-25","url":"https://www.bandsintown.com/e/100451456-pop-rocks-at-hopmonk-tavern-novato?came_from=244","location":{"#type":"Place","name":"HopMonk Tavern Novato","address":"Novato, CA","geo":{"#type":"GeoCoordinates","latitude":38.1074198,"longitude":-122.5697032}},"name":"Pop Rocks","performer":{"#type":"MusicGroup","name":"Pop Rocks","image":"https://photos.bandsintown.com/thumb/8532836.jpeg","url":"https://www.bandsintown.com/a/29109-pop-rocks?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8532836.jpeg"},
Here's my code:
#define url and build url array to cycle through webpages
page = 'https://www.bandsintown.com/?came_from=257&page='
urlBucket = []
for i in range (0,2):
uniqueUrl = page + str(i)
urlBucket.append(uniqueUrl)
# dump response into an array
responseBucket = []
for i in urlBucket:
uniqueResponse = requests.get(i)
responseBucket.append(uniqueResponse)
#Make the 'soup'
soupBucket = []
for i in responseBucket:
individualSoup = BeautifulSoup(i.text, 'html.parser')
soupBucket.append(individualSoup)
# Build an array to hold script
allScript = []
for i in soupBucket:
script = i.find_all("script")[4]
eventsJSON = json.loads(script)
print script
allScript.append(script)
print allScript
Print allScript gives me the following:
[<script type="application/ld+json">[{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100653596-e.r.n.e.s.t.o-at-the-endup?came_from=244","location":{"#type":"Place","name":"The EndUp","address":"SF, CA","geo":{"#type":"GeoCoordinates","latitude":37.7726402,"longitude":-122.4099154}},"name":"E.R.N.E.S.T.O","performer":{"#type":"MusicGroup","name":"E.R.N.E.S.T.O","image":"https://photos.bandsintown.com/thumb/8618862.jpeg","url":"https://www.bandsintown.com/a/4693798-e.r.n.e.s.t.o?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8618862.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012239291-j.j.-grey-and-mofro-at-uptown-theatre-napa?came_from=244","location":{"#type":"Place","name":"Uptown Theatre Napa","address":"Napa, CA","geo":{"#type":"GeoCoordinates","latitude":38.2963465,"longitude":-122.2873698}},"name":"J.J. Grey & Mofro","performer":{"#type":"MusicGroup","name":"J.J. Grey & Mofro","image":"https://photos.bandsintown.com/thumb/219177.jpeg","url":"https://www.bandsintown.com/a/2327212-j.j.-grey-and-mofro?came_from=244"},"image":"https://photos.bandsintown.com/thumb/219177.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012239613-j.j.-grey-at-uptown-theatre-napa?came_from=244","location":{"#type":"Place","name":"Uptown Theatre Napa","address":"Napa, CA","geo":{"#type":"GeoCoordinates","latitude":38.2963465,"longitude":-122.2873698}},"name":"J.J. Grey","performer":{"#type":"MusicGroup","name":"J.J. Grey","image":"","url":"https://www.bandsintown.com/a/12437162-j.j.-grey?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012239435-mofro-at-uptown-theatre-napa?came_from=244","location":{"#type":"Place","name":"Uptown Theatre Napa","address":"Napa, CA","geo":{"#type":"GeoCoordinates","latitude":38.2963465,"longitude":-122.2873698}},"name":"Mofro","performer":{"#type":"MusicGroup","name":"Mofro","image":"","url":"https://www.bandsintown.com/a/71714-mofro?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100542800-brooke-heinichen-at-stuffed?came_from=244","location":{"#type":"Place","name":"Stuffed","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.7485824,"longitude":-122.4184108}},"name":"Brooke Heinichen","performer":{"#type":"MusicGroup","name":"Brooke Heinichen","image":"https://photos.bandsintown.com/thumb/8921909.jpeg","url":"https://www.bandsintown.com/a/14944274-brooke-heinichen?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8921909.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012486121-william-fitzsimmons-at-hopmonk-tavern?came_from=244","location":{"#type":"Place","name":"Hopmonk Tavern","address":"Novato, CA","geo":{"#type":"GeoCoordinates","latitude":38.088489,"longitude":-122.553449}},"name":"William Fitzsimmons","performer":{"#type":"MusicGroup","name":"William Fitzsimmons","image":"https://photos.bandsintown.com/thumb/8852940.jpeg","url":"https://www.bandsintown.com/a/2450-william-fitzsimmons?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8852940.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100581554-kevin-paris-at-acoustic-yoga-#-yoga-source-los-gatos?came_from=244","location":{"#type":"Place","name":"Acoustic Yoga # Yoga Source Los Gatos","address":"Los Gatos, CA","geo":{"#type":"GeoCoordinates","latitude":37.2358078,"longitude":-121.9623751}},"name":"Kevin Paris","performer":{"#type":"MusicGroup","name":"Kevin Paris","image":"https://photos.bandsintown.com/thumb/8419497.jpeg","url":"https://www.bandsintown.com/a/1134314-kevin-paris?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8419497.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100692435-zak-fennie-at-black-stallion-winery?came_from=244","location":{"#type":"Place","name":"Black Stallion Winery","address":"Napa, CA","geo":{"#type":"GeoCoordinates","latitude":38.35983179999999,"longitude":-122.2906388}},"name":"Zak Fennie","performer":{"#type":"MusicGroup","name":"Zak Fennie","image":"https://photos.bandsintown.com/thumb/8851546.jpeg","url":"https://www.bandsintown.com/a/11843851-zak-fennie?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8851546.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100621943-frances-ancheta-at-off-the-grid-at-alameda-south-shore-center?came_from=244","location":{"#type":"Place","name":"Off the Grid at Alameda South Shore Center ","address":"Alameda, CA","geo":{"#type":"GeoCoordinates","latitude":37.7712165,"longitude":-122.2824021}},"name":"Frances Ancheta","performer":{"#type":"MusicGroup","name":"Frances Ancheta","image":"https://photos.bandsintown.com/thumb/8483059.jpeg","url":"https://www.bandsintown.com/a/7762254-frances-ancheta?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8483059.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1013412612-pizza!-at-audio-nightclub?came_from=244","location":{"#type":"Place","name":"Audio Nightclub","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.771362,"longitude":-122.413795}},"name":"Pizza!","performer":{"#type":"MusicGroup","name":"Pizza!","image":"https://photos.bandsintown.com/thumb/161356.jpeg","url":"https://www.bandsintown.com/a/198680-pizza!?came_from=244"},"image":"https://photos.bandsintown.com/thumb/161356.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100372855-ryan-scott-long-at-drake's-barrel-house?came_from=244","location":{"#type":"Place","name":"Drake\u2019s barrel house ","address":"San Leandro, Ca","geo":{"#type":"GeoCoordinates","latitude":37.7249296,"longitude":-122.1560768}},"name":"Ryan Scott Long","performer":{"#type":"MusicGroup","name":"Ryan Scott Long","image":"https://photos.bandsintown.com/thumb/8671372.jpeg","url":"https://www.bandsintown.com/a/3168705-ryan-scott-long?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8671372.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012999412-come-from-away-at-golden-gate-theater?came_from=244","location":{"#type":"Place","name":"Golden Gate Theater","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.7825715,"longitude":-122.4110742}},"name":"Come From Away","performer":{"#type":"MusicGroup","name":"Come From Away","image":"","url":"https://www.bandsintown.com/a/13889714-come-from-away?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100441096-and-then-came-humans-at-drake's-brewing-company?came_from=244","location":{"#type":"Place","name":"Drake\u2019s Brewing Company","address":"San Leandro, Ca","geo":{"#type":"GeoCoordinates","latitude":37.7249296,"longitude":-122.1560768}},"name":"And Then Came Humans","performer":{"#type":"MusicGroup","name":"And Then Came Humans","image":"https://photos.bandsintown.com/thumb/8897159.jpeg","url":"https://www.bandsintown.com/a/13151463-and-then-came-humans?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8897159.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1011601412-man-go-at-el-rio?came_from=244","location":{"#type":"Place","name":"El Rio","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.7467828,"longitude":-122.4193922}},"name":"Man-Go","performer":{"#type":"MusicGroup","name":"Man-Go","image":"","url":"https://www.bandsintown.com/a/3238684-man-go?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1013320819-paul-mehling-at-freight-and-salvage-coffeehouse?came_from=244","location":{"#type":"Place","name":"Freight & Salvage Coffeehouse","address":"Berkeley, CA","geo":{"#type":"GeoCoordinates","latitude":37.8708715,"longitude":-122.2695117}},"name":"Paul Mehling","performer":{"#type":"MusicGroup","name":"Paul Mehling","image":"","url":"https://www.bandsintown.com/a/3307749-paul-mehling?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100672210-dj-spooky-at-catharine-clark-gallery?came_from=244","location":{"#type":"Place","name":"Catharine Clark Gallery","address":"SF, CA","geo":{"#type":"GeoCoordinates","latitude":37.76639,"longitude":-122.40704}},"name":"DJ Spooky","performer":{"#type":"MusicGroup","name":"DJ Spooky","image":"https://photos.bandsintown.com/thumb/7060233.jpeg","url":"https://www.bandsintown.com/a/64476-dj-spooky?came_from=244"},"image":"https://photos.bandsintown.com/thumb/7060233.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012003162-craig-ventresco-at-atlas-cafe?came_from=244","location":{"#type":"Place","name":"Atlas Cafe","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.73189,"longitude":-122.47615}},"name":"Craig Ventresco","performer":{"#type":"MusicGroup","name":"Craig Ventresco","image":"","url":"https://www.bandsintown.com/a/139634-craig-ventresco?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100555258-rusty-jackson-music-at-kawika's-ocean-beach-deli?came_from=244","location":{"#type":"Place","name":"Kawika's Ocean Beach Deli","address":"SF, CA","geo":{"#type":"GeoCoordinates","latitude":37.774627,"longitude":-122.509993}},"name":"Rusty Jackson Music","performer":{"#type":"MusicGroup","name":"Rusty Jackson Music","image":"https://photos.bandsintown.com/thumb/8250003.jpeg","url":"https://www.bandsintown.com/a/9978762-rusty-jackson-music?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8250003.jpeg"}]</script>, <script type="application/ld+json">[{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100653596-e.r.n.e.s.t.o-at-the-endup?came_from=244","location":{"#type":"Place","name":"The EndUp","address":"SF, CA","geo":{"#type":"GeoCoordinates","latitude":37.7726402,"longitude":-122.4099154}},"name":"E.R.N.E.S.T.O","performer":{"#type":"MusicGroup","name":"E.R.N.E.S.T.O","image":"https://photos.bandsintown.com/thumb/8618862.jpeg","url":"https://www.bandsintown.com/a/4693798-e.r.n.e.s.t.o?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8618862.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012239291-j.j.-grey-and-mofro-at-uptown-theatre-napa?came_from=244","location":{"#type":"Place","name":"Uptown Theatre Napa","address":"Napa, CA","geo":{"#type":"GeoCoordinates","latitude":38.2963465,"longitude":-122.2873698}},"name":"J.J. Grey & Mofro","performer":{"#type":"MusicGroup","name":"J.J. Grey & Mofro","image":"https://photos.bandsintown.com/thumb/219177.jpeg","url":"https://www.bandsintown.com/a/2327212-j.j.-grey-and-mofro?came_from=244"},"image":"https://photos.bandsintown.com/thumb/219177.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012239613-j.j.-grey-at-uptown-theatre-napa?came_from=244","location":{"#type":"Place","name":"Uptown Theatre Napa","address":"Napa, CA","geo":{"#type":"GeoCoordinates","latitude":38.2963465,"longitude":-122.2873698}},"name":"J.J. Grey","performer":{"#type":"MusicGroup","name":"J.J. Grey","image":"","url":"https://www.bandsintown.com/a/12437162-j.j.-grey?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012239435-mofro-at-uptown-theatre-napa?came_from=244","location":{"#type":"Place","name":"Uptown Theatre Napa","address":"Napa, CA","geo":{"#type":"GeoCoordinates","latitude":38.2963465,"longitude":-122.2873698}},"name":"Mofro","performer":{"#type":"MusicGroup","name":"Mofro","image":"","url":"https://www.bandsintown.com/a/71714-mofro?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100542800-brooke-heinichen-at-stuffed?came_from=244","location":{"#type":"Place","name":"Stuffed","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.7485824,"longitude":-122.4184108}},"name":"Brooke Heinichen","performer":{"#type":"MusicGroup","name":"Brooke Heinichen","image":"https://photos.bandsintown.com/thumb/8921909.jpeg","url":"https://www.bandsintown.com/a/14944274-brooke-heinichen?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8921909.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012486121-william-fitzsimmons-at-hopmonk-tavern?came_from=244","location":{"#type":"Place","name":"Hopmonk Tavern","address":"Novato, CA","geo":{"#type":"GeoCoordinates","latitude":38.088489,"longitude":-122.553449}},"name":"William Fitzsimmons","performer":{"#type":"MusicGroup","name":"William Fitzsimmons","image":"https://photos.bandsintown.com/thumb/8852940.jpeg","url":"https://www.bandsintown.com/a/2450-william-fitzsimmons?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8852940.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100581554-kevin-paris-at-acoustic-yoga-#-yoga-source-los-gatos?came_from=244","location":{"#type":"Place","name":"Acoustic Yoga # Yoga Source Los Gatos","address":"Los Gatos, CA","geo":{"#type":"GeoCoordinates","latitude":37.2358078,"longitude":-121.9623751}},"name":"Kevin Paris","performer":{"#type":"MusicGroup","name":"Kevin Paris","image":"https://photos.bandsintown.com/thumb/8419497.jpeg","url":"https://www.bandsintown.com/a/1134314-kevin-paris?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8419497.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100692435-zak-fennie-at-black-stallion-winery?came_from=244","location":{"#type":"Place","name":"Black Stallion Winery","address":"Napa, CA","geo":{"#type":"GeoCoordinates","latitude":38.35983179999999,"longitude":-122.2906388}},"name":"Zak Fennie","performer":{"#type":"MusicGroup","name":"Zak Fennie","image":"https://photos.bandsintown.com/thumb/8851546.jpeg","url":"https://www.bandsintown.com/a/11843851-zak-fennie?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8851546.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100621943-frances-ancheta-at-off-the-grid-at-alameda-south-shore-center?came_from=244","location":{"#type":"Place","name":"Off the Grid at Alameda South Shore Center ","address":"Alameda, CA","geo":{"#type":"GeoCoordinates","latitude":37.7712165,"longitude":-122.2824021}},"name":"Frances Ancheta","performer":{"#type":"MusicGroup","name":"Frances Ancheta","image":"https://photos.bandsintown.com/thumb/8483059.jpeg","url":"https://www.bandsintown.com/a/7762254-frances-ancheta?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8483059.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1013412612-pizza!-at-audio-nightclub?came_from=244","location":{"#type":"Place","name":"Audio Nightclub","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.771362,"longitude":-122.413795}},"name":"Pizza!","performer":{"#type":"MusicGroup","name":"Pizza!","image":"https://photos.bandsintown.com/thumb/161356.jpeg","url":"https://www.bandsintown.com/a/198680-pizza!?came_from=244"},"image":"https://photos.bandsintown.com/thumb/161356.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100372855-ryan-scott-long-at-drake's-barrel-house?came_from=244","location":{"#type":"Place","name":"Drake\u2019s barrel house ","address":"San Leandro, Ca","geo":{"#type":"GeoCoordinates","latitude":37.7249296,"longitude":-122.1560768}},"name":"Ryan Scott Long","performer":{"#type":"MusicGroup","name":"Ryan Scott Long","image":"https://photos.bandsintown.com/thumb/8671372.jpeg","url":"https://www.bandsintown.com/a/3168705-ryan-scott-long?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8671372.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012999412-come-from-away-at-golden-gate-theater?came_from=244","location":{"#type":"Place","name":"Golden Gate Theater","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.7825715,"longitude":-122.4110742}},"name":"Come From Away","performer":{"#type":"MusicGroup","name":"Come From Away","image":"","url":"https://www.bandsintown.com/a/13889714-come-from-away?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100441096-and-then-came-humans-at-drake's-brewing-company?came_from=244","location":{"#type":"Place","name":"Drake\u2019s Brewing Company","address":"San Leandro, Ca","geo":{"#type":"GeoCoordinates","latitude":37.7249296,"longitude":-122.1560768}},"name":"And Then Came Humans","performer":{"#type":"MusicGroup","name":"And Then Came Humans","image":"https://photos.bandsintown.com/thumb/8897159.jpeg","url":"https://www.bandsintown.com/a/13151463-and-then-came-humans?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8897159.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1011601412-man-go-at-el-rio?came_from=244","location":{"#type":"Place","name":"El Rio","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.7467828,"longitude":-122.4193922}},"name":"Man-Go","performer":{"#type":"MusicGroup","name":"Man-Go","image":"","url":"https://www.bandsintown.com/a/3238684-man-go?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1013320819-paul-mehling-at-freight-and-salvage-coffeehouse?came_from=244","location":{"#type":"Place","name":"Freight & Salvage Coffeehouse","address":"Berkeley, CA","geo":{"#type":"GeoCoordinates","latitude":37.8708715,"longitude":-122.2695117}},"name":"Paul Mehling","performer":{"#type":"MusicGroup","name":"Paul Mehling","image":"","url":"https://www.bandsintown.com/a/3307749-paul-mehling?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100672210-dj-spooky-at-catharine-clark-gallery?came_from=244","location":{"#type":"Place","name":"Catharine Clark Gallery","address":"SF, CA","geo":{"#type":"GeoCoordinates","latitude":37.76639,"longitude":-122.40704}},"name":"DJ Spooky","performer":{"#type":"MusicGroup","name":"DJ Spooky","image":"https://photos.bandsintown.com/thumb/7060233.jpeg","url":"https://www.bandsintown.com/a/64476-dj-spooky?came_from=244"},"image":"https://photos.bandsintown.com/thumb/7060233.jpeg"},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/1012003162-craig-ventresco-at-atlas-cafe?came_from=244","location":{"#type":"Place","name":"Atlas Cafe","address":"San Francisco, CA","geo":{"#type":"GeoCoordinates","latitude":37.73189,"longitude":-122.47615}},"name":"Craig Ventresco","performer":{"#type":"MusicGroup","name":"Craig Ventresco","image":"","url":"https://www.bandsintown.com/a/139634-craig-ventresco?came_from=244"},"image":""},{"#context":"http://schema.org","#type":"MusicEvent","startDate":"2019-01-26","endDate":"2019-01-26","url":"https://www.bandsintown.com/e/100555258-rusty-jackson-music-at-kawika's-ocean-beach-deli?came_from=244","location":{"#type":"Place","name":"Kawika's Ocean Beach Deli","address":"SF, CA","geo":{"#type":"GeoCoordinates","latitude":37.774627,"longitude":-122.509993}},"name":"Rusty Jackson Music","performer":{"#type":"MusicGroup","name":"Rusty Jackson Music","image":"https://photos.bandsintown.com/thumb/8250003.jpeg","url":"https://www.bandsintown.com/a/9978762-rusty-jackson-music?came_from=244"},"image":"https://photos.bandsintown.com/thumb/8250003.jpeg"}]</script>]
But, printing eventsJSON gives me an error:
TypeError: expected string or buffer
I want to be able to build a new JSON based on specific attributes in eventsJsonLd, ie "startDate", "name", etc. Can anyone tell me where I'm going wrong? Thanks in advance.
You are passing the script tag into json.loads, this is not string but an object of the bs4.element.Tag class.
script = i.find_all("script")[4]
print(type(script))
Output
<class 'bs4.element.Tag'>
You need to get the text from the tag and pass it to json.loads
eventsJSON = json.loads(script.text)
Note:
The current url you try (https://www.bandsintown.com/?came_from=257&page=0) has the contents of that script tag as empty, i was able to get an output for a different url (https://www.bandsintown.com/a/29109-pop-rocks) of the same domain.
print(eventsJSON[0])
Gave an output
{u'startDate': u'2019-02-15T21:00:00', u'performer': {u'url': u'https://www.bandsintown.com/a/29109-pop-rocks?came_from=244', u'image': u'https://photos.bandsintown.com/thumb/8532836.jpeg', u'#type': u'MusicGroup', u'name': u'Pop Rocks'}, u'name': u'Pop Rocks', u'url': u'https://www.bandsintown.com/e/100544648-pop-rocks-at-the-chapel?came_from=244', u'image': u'https://photos.bandsintown.com/thumb/8532836.jpeg', u'location': {u'address': u'San Francisco, CA', u'geo': {u'latitude': 37.7485824, u'#type': u'GeoCoordinates', u'longitude': -122.4184108}, u'#type': u'Place', u'name': u'The Chapel'}, u'#context': u'http://schema.org', u'#type': u'MusicEvent', u'description': u'Pop Rocks at The Chapel 2019-02-15T21:00:00'}
Related
Error when trying process .json files to pandas dataframe
I'm trying to merge various .json files so I can later run sentiment analysis on them. I tried already other approaches but they always end up with an error. I checked whether the .json is correctly formatted and can't find any issues there. I also attached an example of a .json file. Error message is attached below my code. import glob import json # list all files containing News from Guardian API files = list(glob.iglob('/Users/xxx/tempdata/articles_data/*.json')) news_data = [] for file in files: news_file = open(file, "r", encoding = 'utf-8') # Read in news and store in list: news_data for line in news_file: news = json.loads(line) news_data.append(news) news_file.close() Updated Error Output AttributeError Traceback (most recent call last) <ipython-input-86-3019ee85b15b> in <module> 12 # Read in news and store in list: news_data 13 for line in news_file: ---> 14 news = json.load(line) 15 news_data.append(news) 16 ~/opt/anaconda3/lib/python3.8/json/__init__.py in load(fp, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw) 291 kwarg; otherwise ``JSONDecoder`` is used. 292 """ --> 293 return loads(fp.read(), 294 cls=cls, object_hook=object_hook, 295 parse_float=parse_float, parse_int=parse_int, AttributeError: 'str' object has no attribute 'read' 2019-11-01.json { "id":"business/2019/nov/01/google-snaps-up-fitbit-for-21bn", "type":"article", "sectionId":"business", "sectionName":"Business", "webPublicationDate":"2019-11-01T14:26:19Z", "webTitle":"Google snaps up Fitbit for $2.1bn", "webUrl":"https://www.theguardian.com/business/2019/nov/01/google-snaps-up-fitbit-for-21bn", "apiUrl":"https://content.guardianapis.com/business/2019/nov/01/google-snaps-up-fitbit-for-21bn", "fields":{ "headline":"Google snaps up Fitbit for $2.1bn", "standfirst":"<p>Takeover allows web giant to take on Apple in fast-growing smartwatch and wearables business</p>", "trailText":"Takeover allows web giant to take on Apple in fast-growing smartwatch and wearables business", "byline":"Kalyeena Makortoff", "main":"<figure class=\"element element-image\" data-media-id=\"fc8abb0f70105fcab3aee86dea6c89e211337660\"> <img src=\"https://media.guim.co.uk/fc8abb0f70105fcab3aee86dea6c89e211337660/0_158_3571_2143/1000.jpg\" alt=\"The wireless activity tracker Zip by Fitbit Inc\" width=\"1000\" height=\"600\" class=\"gu-image\" /> <figcaption> <span class=\"element-image__caption\">The wireless activity tracker Zip by Fitbit Inc. Google has confirmed it will buy Fitbit for $2.1bn.</span> <span class=\"element-image__credit\">Photograph: Franck Robichon/EPA</span> </figcaption> </figure>", "body":"<p>Google has snapped up the Fitbit... ", "newspaperPageNumber":"38", "wordcount":"679", "firstPublicationDate":"2019-11-01T14:25:58Z", "isInappropriateForSponsorship":"false", "isPremoderated":"false", "lastModified":"2019-11-01T18:56:38Z", "newspaperEditionDate":"2019-11-02T00:00:00Z", "productionOffice":"UK", "publication":"The Guardian", "shortUrl":"https://gu.com/p/cjeze", "shouldHideAdverts":"false", "showInRelatedContent":"true", "thumbnail":"https://media.guim.co.uk/fc8abb0f70105fcab3aee86dea6c89e211337660/0_158_3571_2143/500.jpg", "legallySensitive":"false", "lang":"en", "isLive":"true", "bodyText":"Google has snapped up the Fitbit activity tracker business in a $2.1bn (\u00a31.6bn) deal that will enable the search giant to go toe-to-toe with Apple in the fast-growing smartwatch and wearables business..." , "charCount":"4149", "shouldHideReaderRevenue":"false", "showAffiliateLinks":"false", "bylineHtml":"Kalyeena Makortoff" }, "isHosted":false, "pillarId":"pillar/news", "pillarName":"News" },
If you are reading json from file, you should use json.load instead of json.loads. json.loads is for reading JSON from string. See json.load documentation. For example: import json with open('ts.json', 'r') as f: content = json.load(f) print(content)
How to retrieve data from a json
I retrieved a dataset from a news API in JSON format. I want to extract the news description from the JSON data. This is my code:- import requests import json url = ('http://newsapi.org/v2/top-headlines?' 'country=us&' 'apiKey=608bf565c67f4d99994c08d74db82f54') response = requests.get(url) di=response.json() di = json.dumps(di) for di['articles'] in di: print(article['title']) The dataset looks like this:- {'status': 'ok', 'totalResults': 38, 'articles': [ {'source': {'id': 'the-washington-post', 'name': 'The Washington Post'}, 'author': 'Derek Hawkins, Marisa Iati', 'title': 'Coronavirus updates: Texas, Florida and Arizona officials say early reopenings fueled an explosion of cases - The Washington Post', 'description': 'Local officials in states with surging coronavirus cases issued dire warnings Sunday about the spread of infections, saying the virus was rapidly outpacing containment efforts.', 'url': 'https://www.washingtonpost.com/nation/2020/07/05/coronavirus-update-us/', 'urlToImage': 'https://www.washingtonpost.com/wp-apps/imrs.php?src=https://arc-anglerfish-washpost-prod-washpost.s3.amazonaws.com/public/K3UMAKF6OMI6VF6BNTYRN77CNQ.jpg&w=1440', 'publishedAt': '2020-07-05T18:32:44Z', 'content': 'Here are some significant developments:\r\n<ul><li>The rolling seven-day average for daily new cases in the United States reached a record high for the 27th day in a row, climbing to 48,606 on Sunday, … [+5333 chars]'}]) Please guide me with this!
There are few corrections needed in your code.. below code should work and i have removed API KEY in answer make sure that you add one before testing import requests import json url = ('http://newsapi.org/v2/top-headlines?' 'country=us&' 'apiKey=<API KEY>') di=response.json() #You don't need to dump json that is already in json format #di = json.dumps(di) #your loop is not correctly defined, below is correct way to do it for article in di['articles']: print(article['title'])
response.json {'status': 'ok', 'totalResults': 38, 'articles': [ {'source': {'id': 'the-washington-post', 'name': 'The Washington Post'}, 'author': 'Derek Hawkins, Marisa Iati', 'title': 'Coronavirus updates: Texas, Florida and Arizona officials say early reopenings fueled an explosion of cases - The Washington Post', 'description': 'Local officials in states with surging coronavirus cases issued dire warnings Sunday about the spread of infections, saying the virus was rapidly outpacing containment efforts.', 'url': 'https://www.washingtonpost.com/nation/2020/07/05/coronavirus-update-us/', 'urlToImage': 'https://www.washingtonpost.com/wp-apps/imrs.php?src=https://arc-anglerfish-washpost-prod-washpost.s3.amazonaws.com/public/K3UMAKF6OMI6VF6BNTYRN77CNQ.jpg&w=1440', 'publishedAt': '2020-07-05T18:32:44Z', 'content': 'Here are some significant developments:\r\n<ul><li>The rolling seven-day average for daily new cases in the United States reached a record high for the 27th day in a row, climbing to 48,606 on Sunday, … [+5333 chars]'}]} Code: di = response.json() # Understand that 'di' is of type 'dictionary', key-value pair for i in di["articles"]: print(i["description"]) "articles" is one of the keys of dictionary di, It's corresponding value is of type list. "description" , which you are looking is part of this list (value of "articles"). Further list contains the dictionary (key-value pair).You can access from key - description
Add a COMMA after a CURLY Brace For JSON Format
Hello there i am working on a dataset but its not formatted correctly. It's missing its Square brackets and comma after every object Example: {"is_sarcastic": 1, "headline": "thirtysomething scientists unveil doomsday clock of hair loss", "article_link": "something"} {"is_sarcastic": 0, "headline": "dem rep. totally nails why congress is falling short on gender, racial equality", "article_link": "somethingelse"} I want to format it such that it turns to this: [{"is_sarcastic": 1, "headline": "thirtysomething scientists unveil doomsday clock of hair loss", "article_link": "something"}, {"is_sarcastic": 0, "headline": "dem rep. totally nails why congress is falling short on gender, racial equality", "article_link": "somethingelse"}] I am using Python 3.x to achieve this task.
you can run the following python script, it'll output a file containing the output you desire. import json dataJson = [] with open('data.json') as f: for jsonObj in f: dataDict = json.loads(jsonObj) dataJson.append(dataDict) #print (dataJson) with open('data2.json', 'w') as jsonfile: json.dump(dataJson, jsonfile) where data.json is the name of the file containing the dataset
Unicode characters incorrect when parsing JSON
This is the link of JSON sample MYSAMPLEJSON This is sample [{"{\"Title\":\"Close-Up\",\"Year\":\"1990\",\"Rated\":\"NOT RATED\",\"Released\":\"30 Oct 1991\",\"Runtime\":\"98 min\",\"Genre\":\"Documentary, Biography, Crime\",\"Director\":\"Abbas Kiarostami\",\"Writer\":\"Abbas Kiarostami\",\"Actors\":\"Matthew Barney, Björk, Mohsen Makhmalbaf, Abolfazl Ahankhah, Mehrdad Ahankhah\",\"Plot\":\"The true story of Hossain Sabzian that impersonated the director Mohsen Makhmalbaf to convince a family they would star in his so-called new film.\",\"Language\":\"Persian, Azerbaijani\",\"Country\":\"Iran\",\"Awards\":\"2 wins.\",\"Poster\":\"https://m.media-amazon.com/images/M/MV5BMzE4Mjc0MjI1N15BMl5BanBnXkFtZTcwNjI3MzEzMw##._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"8.3/10\"},{\"Source\":\"Rotten Tomatoes\",\"Value\":\"88%\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"8.3\",\"imdbVotes\":\"11,546\",\"imdbID\":\"tt0100234\",\"Type\":\"movie\",\"DVD\":\"19 Feb 2002\",\"BoxOffice\":\"N/A\",\"Production\":\"Zeitgeist Films\",\"Website\":\"http://www.zeitgeistfilm.com/current/closeup/closeup.html\",\"Response\":\"True\"}":true},{"{\"Title\":\"Life on Earth\",\"Year\":\"1998\",\"Rated\":\"N/A\",\"Released\":\"09 Jun 1999\",\"Runtime\":\"61 min\",\"Genre\":\"Comedy, Drama\",\"Director\":\"Abderrahmane Sissako\",\"Writer\":\"Abderrahmane Sissako\",\"Actors\":\"Abderrahmane Sissako, Nana Baby, Mohamed Sissako, Bourama Coulibaly\",\"Plot\":\"In the last days of 1999, after a few shots of a French supermarket, abundant in food and color, we hear Dramane compose a letter home to his father in Mali whom he then visits in the ...\",\"Language\":\"French, Bambara\",\"Country\":\"Mali, Mauritania, France\",\"Awards\":\"10 wins.\",\"Poster\":\"https://images-na.ssl-images-amazon.com/images/M/MV5BNDAxMzg2MDE1M15BMl5BanBnXkFtZTcwMzU3MDMyMQ##._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"6.8/10\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"6.8\",\"imdbVotes\":\"297\",\"imdbID\":\"tt0157160\",\"Type\":\"movie\",\"DVD\":\"N/A\",\"BoxOffice\":\"N/A\",\"Production\":\"N/A\",\"Website\":\"N/A\",\"Response\":\"True\"}":true},{"{\"Title\":\"Waiting for Happiness\",\"Year\":\"2002\",\"Rated\":\"N/A\",\"Released\":\"15 Jan 2003\",\"Runtime\":\"90 min\",\"Genre\":\"Drama, Music\",\"Director\":\"Abderrahmane Sissako\",\"Writer\":\"Abderrahmane Sissako\",\"Actors\":\"Khatra Ould Abder Kader, Maata Ould Mohamed Abeid, Mohamed Mahmoud Ould Mohamed, Fatimetou Mint Ahmeda\",\"Plot\":\"The story of two people who cross paths in Nouhadhibou.\",\"Language\":\"French, Hassanya, Mandarin\",\"Country\":\"France, Mauritania\",\"Awards\":\"8 wins & 3 nominations.\",\"Poster\":\"https://images-na.ssl-images-amazon.com/images/M/MV5BMTI1MzA2NDc3Nl5BMl5BanBnXkFtZTcwNjcxNzY5MQ##._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"6.8/10\"},{\"Source\":\"Rotten Tomatoes\",\"Value\":\"76%\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"6.8\",\"imdbVotes\":\"775\",\"imdbID\":\"tt0308363\",\"Type\":\"movie\",\"DVD\":\"11 Jun 2007\",\"BoxOffice\":\"N/A\",\"Production\":\"New Yorker Films\",\"Website\":\"N/A\",\"Response\":\"True\"}":true},{"{\"Title\":\"Delbaran\",\"Year\":\"2001\",\"Rated\":\"N/A\",\"Released\":\"27 Mar 2002\",\"Runtime\":\"96 min\",\"Genre\":\"Drama\",\"Director\":\"Abolfazl Jalili\",\"Writer\":\"Abolfazl Jalili, Reza Saberi (story)\",\"Actors\":\"Kaim Alizadeh, Rahmatollah Ebrahimi, Hossein Hashemian, Ahmad Mahdavi\",\"Plot\":\"Acclaimed director Abolfazl Jalili offers a compassionate story of the young Afghan refugee who lives illegally in Iran. 14-year-old Kaim drifts to the Delbaran crossing on the Afghan-Iran ...\",\"Language\":\"Persian\",\"Country\":\"Iran, Japan\",\"Awards\":\"5 wins & 3 nominations.\",\"Poster\":\"https://images-na.ssl-images-amazon.com/images/M/MV5BMjA4OTQyNTQzM15BMl5BanBnXkFtZTcwMTM0MDYxMg##._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"6.8/10\"},{\"Source\":\"Rotten Tomatoes\",\"Value\":\"60%\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"6.8\",\"imdbVotes\":\"228\",\"imdbID\":\"tt0293082\",\"Type\":\"movie\",\"DVD\":\"23 Dec 2008\",\"BoxOffice\":\"N/A\",\"Production\":\"Facets\",\"Website\":\"N/A\",\"Response\":\"True\"}":true},{"{\"Title\":\"A Horrible Way to Die\",\"Year\":\"2010\",\"Rated\":\"R\",\"Released\":\"19 Aug 2011\",\"Runtime\":\"87 min\",\"Genre\":\"Drama, Horror, Thriller\",\"Director\":\"Adam Wingard\",\"Writer\":\"Simon Barrett\",\"Actors\":\"AJ Bowen, Amy Seimetz, Joe Swanberg, Brandon Carroll\",\"Plot\":\"An escaped murderer is in pursuit of his ex-girlfriend, who has fled to start a new life in a small town.\",\"Language\":\"English\",\"Country\":\"USA\",\"Awards\":\"3 wins.\",\"Poster\":\"https://m.media-amazon.com/images/M/MV5BMTAzMjAyMzg2MzheQTJeQWpwZ15BbWU3MDg2NTc1MzY#._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"5.3/10\"},{\"Source\":\"Rotten Tomatoes\",\"Value\":\"67%\"},{\"Source\":\"Metacritic\",\"Value\":\"52/100\"}],\"Metascore\":\"52\",\"imdbRating\":\"5.3\",\"imdbVotes\":\"3,036\",\"imdbID\":\"tt1617620\",\"Type\":\"movie\",\"DVD\":\"06 Sep 2011\",\"BoxOffice\":\"N/A\",\"Production\":\"Anchor Bay Entertainment\",\"Website\":\"http://ahwtd.blogspot.com/\",\"Response\":\"True\"}":true},{"{\"Title\":\"Towelhead\",\"Year\":\"2007\",\"Rated\":\"R\",\"Released\":\"26 Sep 2008\",\"Runtime\":\"124 min\",\"Genre\":\"Drama\",\"Director\":\"Alan Ball\",\"Writer\":\"Alan Ball (written for the screen by), Alicia Erian (novel)\",\"Actors\":\"Summer Bishil, Chris Messina, Maria Bello, Peter Macdissi\",\"Plot\":\"A young Arab-American girl struggles with her sexual obsession, a bigoted Army reservist and her strict father during the Gulf War.\",\"Language\":\"English, French, Arabic, Spanish\",\"Country\":\"USA\",\"Awards\":\"1 win & 3 nominations.\",\"Poster\":\"https://ia.media-imdb.com/images/M/MV5BMTI3MTY3OTM5MF5BMl5BanBnXkFtZTcwOTQ4MTA3MQ##._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"7.0/10\"},{\"Source\":\"Rotten Tomatoes\",\"Value\":\"49%\"},{\"Source\":\"Metacritic\",\"Value\":\"57/100\"}],\"Metascore\":\"57\",\"imdbRating\":\"7.0\",\"imdbVotes\":\"10,855\",\"imdbID\":\"tt0787523\",\"Type\":\"movie\",\"DVD\":\"30 Dec 2008\",\"BoxOffice\":\"$277,354\",\"Production\":\"Warner Independent\",\"Website\":\"http://www.towelhead-themovie.com/\",\"Response\":\"True\"}":true},{"{\"Title\":\"Moloch\",\"Year\":\"1999\",\"Rated\":\"N/A\",\"Released\":\"13 Oct 1999\",\"Runtime\":\"108 min\",\"Genre\":\"Drama\",\"Director\":\"Aleksandr Sokurov\",\"Writer\":\"Yuriy Arabov, Marina Koreneva\",\"Actors\":\"Elena Rufanova, Leonid Mozgovoy, Irina Sokolova, Yelena Spiridonova\",\"Plot\":\"In 1942, in Bavaria, Eva Braun is alone, when Adolf Hitler arrives with Dr. Josef Goebbels and his wife Magda Goebbels and Martin Bormann to spend a couple of days without talking politics.\",\"Language\":\"German\",\"Country\":\"Russia, Germany, Japan, Italy, France\",\"Awards\":\"7 wins & 8 nominations.\",\"Poster\":\"https://images-na.ssl-images-amazon.com/images/M/MV5BMTU5ODUzNTE1MV5BMl5BanBnXkFtZTcwNjQzNjgyMQ##._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"6.8/10\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"6.8\",\"imdbVotes\":\"1,898\",\"imdbID\":\"tt0199777\",\"Type\":\"movie\",\"DVD\":\"08 Mar 2005\",\"BoxOffice\":\"N/A\",\"Production\":\"N/A\",\"Website\":\"N/A\",\"Response\":\"True\"}":true},{"{\"Title\":\"Mother and Son\",\"Year\":\"1997\",\"Rated\":\"N/A\",\"Released\":\"06 Feb 1998\",\"Runtime\":\"73 min\",\"Genre\":\"Drama\",\"Director\":\"Aleksandr Sokurov\",\"Writer\":\"Yuriy Arabov\",\"Actors\":\"Aleksei Ananishnov, Gudrun Geyer\",\"Plot\":\"A slow and poignant story of love and patience told via a dying mother nursed by her devoted son. The simple narrative is a thread woven among the deeply spiritual images of the countryside...\",\"Language\":\"Russian\",\"Country\":\"Russia, Germany\",\"Awards\":\"4 wins & 4 nominations.\",\"Poster\":\"https://images-na.ssl-images-amazon.com/images/M/MV5BMTQwNTk4Nzk1MV5BMl5BanBnXkFtZTcwOTc0MDAyMQ##._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"7.6/10\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"7.6\",\"imdbVotes\":\"3,143\",\"imdbID\":\"tt0119711\",\"Type\":\"movie\",\"DVD\":\"N/A\",\"BoxOffice\":\"N/A\",\"Production\":\"N/A\",\"Website\":\"N/A\",\"Response\":\"True\"}":true},{"{\"Title\":\"Russian Ark\",\"Year\":\"2002\",\"Rated\":\"NOT RATED\",\"Released\":\"19 Apr 2003\",\"Runtime\":\"99 min\",\"Genre\":\"Drama, Fantasy, History\",\"Director\":\"Aleksandr Sokurov\",\"Writer\":\"Boris Khaimsky (dialogue), Anatoli Nikiforov, Svetlana Proskurina (dialogue), Aleksandr Sokurov (dialogue), Aleksandr Sokurov\",\"Actors\":\"Sergey Dreyden, Mariya Kuznetsova, Leonid Mozgovoy, Mikhail Piotrovsky\",\"Plot\":\"A 19th century French aristocrat, notorious for his scathing memoirs about life in Russia, travels through the Russian State Hermitage Museum and encounters historical figures from the last 200+ years.\",\"Language\":\"Russian, Persian\",\"Country\":\"Russia, Germany, Japan, Canada, Finland, Denmark\",\"Awards\":\"10 wins & 15 nominations.\",\"Poster\":\"https://m.media-amazon.com/images/M/MV5BMjEyNDEwOTU0NV5BMl5BanBnXkFtZTYwNjk0NTk5._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"7.4/10\"},{\"Source\":\"Rotten Tomatoes\",\"Value\":\"89%\"},{\"Source\":\"Metacritic\",\"Value\":\"86/100\"}],\"Metascore\":\"86\",\"imdbRating\":\"7.4\",\"imdbVotes\":\"16,440\",\"imdbID\":\"tt0318034\",\"Type\":\"movie\",\"DVD\":\"09 Sep 2003\",\"BoxOffice\":\"$37,439\",\"Production\":\"Wellspring Media\",\"Website\":\"http://www.russianark.spb.ru/eng/index.html\",\"Response\":\"True\"}":true},{"{\"Title\":\"The Father\",\"Year\":\"1996\",\"Rated\":\"N/A\",\"Released\":\"10 Feb 1999\",\"Runtime\":\"91 min\",\"Genre\":\"Drama\",\"Director\":\"Majid Majidi\",\"Writer\":\"Majid Majidi, Mehdi Shojai\",\"Actors\":\"Hossein Abedini, Mohammad Kasebi, Parivash Nazarieh, Hassan Sadeghi\",\"Plot\":\"Mehrollah is a 14-year-old boy who is forced to find a job to support his family after his father dies. He travels to the southern parts of Iran, looking for work. Upon his return to his hometown, he notices certain changes in his family.\",\"Language\":\"Persian\",\"Country\":\"Iran\",\"Awards\":\"9 wins & 3 nominations.\",\"Poster\":\"https://images-na.ssl-images-amazon.com/images/M/MV5BMTUxMTI1OTAyOF5BMl5BanBnXkFtZTcwOTc3OTQyMg##._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"7.6/10\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"7.6\",\"imdbVotes\":\"1,122\",\"imdbID\":\"tt0117315\",\"Type\":\"movie\",\"DVD\":\"27 Jan 2009\",\"BoxOffice\":\"N/A\",\"Production\":\"N/A\",\"Website\":\"N/A\",\"Response\":\"True\"}":true},{"{\"Title\":\"Inside\",\"Year\":\"2007\",\"Rated\":\"R\",\"Released\":\"13 Jun 2007\",\"Runtime\":\"82 min\",\"Genre\":\"Horror\",\"Director\":\"Alexandre Bustillo, Julien Maury\",\"Writer\":\"Julien Maury (by), Alexandre Bustillo (by), Alexandre Bustillo (scenario, adaptation and dialogue)\",\"Actors\":\"Alysson Paradis, Jean-Baptiste Tabourin, Claude Lulé, Dominique Frot\",\"Plot\":\"Four months after the death of her husband, a woman on the brink of motherhood is tormented in her home by a strange woman who wants her unborn baby.\",\"Language\":\"French\",\"Country\":\"France\",\"Awards\":\"9 wins & 5 nominations.\",\"Poster\":\"https://m.media-amazon.com/images/M/MV5BNzA2OTM3NGMtYTVkZC00ZGM2LWEzNzYtMjk1OGEwYjFhZjA0XkEyXkFqcGdeQXVyMTQxNzMzNDI#._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"6.8/10\"},{\"Source\":\"Rotten Tomatoes\",\"Value\":\"83%\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"6.8\",\"imdbVotes\":\"33,241\",\"imdbID\":\"tt0856288\",\"Type\":\"movie\",\"DVD\":\"15 Apr 2008\",\"BoxOffice\":\"N/A\",\"Production\":\"The Weinstein Company\",\"Website\":\"http://www.alinterieur.fr/\",\"Response\":\"True\"}":true},{"{\"Title\":\"Matthew Barney: No Restraint\",\"Year\":\"2006\",\"Rated\":\"N/A\",\"Released\":\"04 Oct 2008\",\"Runtime\":\"72 min\",\"Genre\":\"Documentary\",\"Director\":\"Alison Chernick\",\"Writer\":\"Alison Chernick\",\"Actors\":\"Matthew Barney, Björk\",\"Plot\":\"How does artist Matthew Barney use 45,000 pounds of petroleum jelly, a factory whaling vessel and traditional Japanese rituals to create his latest art project? Barney plowed the waters off...\",\"Language\":\"Japanese, English\",\"Country\":\"USA\",\"Awards\":\"1 nomination.\",\"Poster\":\"https://images-na.ssl-images-amazon.com/images/M/MV5BMTk1OTY5OTc3Nl5BMl5BanBnXkFtZTcwMDc2Mjc0MQ##._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"6.0/10\"},{\"Source\":\"Rotten Tomatoes\",\"Value\":\"52%\"},{\"Source\":\"Metacritic\",\"Value\":\"59/100\"}],\"Metascore\":\"59\",\"imdbRating\":\"6.0\",\"imdbVotes\":\"108\",\"imdbID\":\"tt0481574\",\"Type\":\"movie\",\"DVD\":\"01 May 2007\",\"BoxOffice\":\"N/A\",\"Production\":\"IFC First Take\",\"Website\":\"http://www.matthewbarneynorestraint.com/\",\"Response\":\"True\"}":true},{"{\"Title\":\"Vegas: Based on a True Story\",\"Year\":\"2008\",\"Rated\":\"N/A\",\"Released\":\"24 Apr 2009\",\"Runtime\":\"102 min\",\"Genre\":\"Drama\",\"Director\":\"Amir Naderi\",\"Writer\":\"Susan Brennan (screenplay), Bliss Esposito (screenplay), Charlie Lake Keaton (screenplay), Amir Naderi (screenplay)\",\"Actors\":\"Mark Greenfield, Alexis Hart, Nancy La Scala, Zach Thomas\",\"Plot\":\"The film takes place away from the glittering strip of mega casinos, but the greed of Sin City is just as pervasive on the desert outskirts. This is where a happy family learns of a ...\",\"Language\":\"English\",\"Country\":\"USA\",\"Awards\":\"2 wins & 2 nominations.\",\"Poster\":\"https://images-na.ssl-images-amazon.com/images/M/MV5BMjE1MzYzMDIyMV5BMl5BanBnXkFtZTgwNjc2NjU5OTE#._V1_SX300.jpg\",\"Ratings\":[{\"Source\":\"Internet Movie Database\",\"Value\":\"7.0/10\"}],\"Metascore\":\"N/A\",\"imdbRating\":\"7.0\",\"imdbVotes\":\"141\",\"imdbID\":\"tt1283971\",\"Type\":\"movie\",\"DVD\":\"06 Jul 2010\",\"BoxOffice\":\"N/A\",\"Production\":\"Alphaville Films\",\"Website\":\"N/A\",\"Response\":\"True\"}":true}] And I have used this code to parse json Sub Test() Dim ky1 As Collection Dim ky2 As Collection Dim ky3 As Dictionary Dim fso As Object Dim jsonFile As Object Dim Json As Object Dim arr() As String Dim jsonText As String Dim i As Long Dim j As Long Set fso = CreateObject("Scripting.FileSystemObject") Set jsonFile = fso.OpenTextFile(ThisWorkbook.Path & "\Sample.json") jsonText = jsonFile.ReadAll Set Json = JsonConverter.ParseJson(jsonText) Set ky1 = Json("results") Set ky2 = ky1(1)("allTests") ReDim arr(1 To ky2.Count) For i = 1 To ky2.Count Set ky3 = ky2(i) If InStr(ky3.keys(0), "Movie not found") = 0 Then j = j + 1 arr(j) = ky3.keys(0) End If Next i Application.ScreenUpdating = False Sheets("Sheet1").Range("A1").Resize(UBound(arr)) = Application.Transpose(arr) Application.ScreenUpdating = True End Sub And it is working well and as expected. The only problem is that I found out incorrect in some of the results that have unicode characters for example debug the macro and find arr(12) >> it is supposed that there is the value "Matthew Barney, Björk\" but it appears on worksheet incorrectly As for the simple sample try arr(1) Thanks advanced for help
Use ADODB.Stream to give UTF-8 encoding, also use a strongy typed objects to make life easier. Add reference to your excel project in VBA editor. Tools/References: [x]Microsoft Scripting Runtime, [x]Microsoft ActiveX Data Object x.x Library where ADO library select most recent one. Public Sub parseJSONFile() Dim ky1 As Collection Dim ky2 As Collection Dim ky3 As Dictionary 'Dim fso As Scripting.FileSystemObject 'Dim jsonFile As Scripting.TextStream Dim adofs As ADODB.Stream Dim Json As Object Dim arr() As String Dim jsonText As String Dim i As Long Dim j As Long 'Set fso = New Scripting.FileSystemObject 'Set jsonFile = fso.OpenTextFile(ThisWorkbook.Path & "\Sample.json") 'jsonText = jsonFile.ReadAll Set adofs = New ADODB.Stream adofs.Charset = "UTF-8" adofs.Open Call adofs.LoadFromFile(ThisWorkbook.Path & "\Sample.json") jsonText = adofs.ReadText adofs.Close Set adofs = Nothing Set Json = JsonConverter.ParseJson(jsonText) ...and so on do the magic... End Sub
Applescript JSON field extraction
Hi I´m in need of help to extract a field from a JSON Helper return Using the following syntax tell application "JSON Helper" set Resultado to (fetch JSON from Request) end tell I get the following result from a Google Geocoding API request. {results:{{formatted_address:"Rua Dr. Carmelo D'Agostino, 628 - Jardim Rincão, São Paulo - SP, 02991-040, Brazil", partial_match:true, address_components:{{short_name:"628", long_name:"628", types:{"street_number"}}, {short_name:"Rua Dr. Carmelo D'Agostino", long_name:"Rua Doutor Carmelo D'Agostino", types:{"route"}}, {short_name:"Jardim Rincão", long_name:"Jardim Rincão", types:{"political", "sublocality", "sublocality_level_1"}}, {short_name:"São Paulo", long_name:"São Paulo", types:{"administrative_area_level_2", "political"}}, {short_name:"SP", long_name:"São Paulo", types:{"administrative_area_level_1", "political"}}, {short_name:"BR", long_name:"Brazil", types:{"country", "political"}}, {short_name:"02991-040", long_name:"02991-040", types:{"postal_code"}}}, geometry:{viewport:{northeast:{lat:-23.432391219708, lng:-46.725645019708}, southwest:{lat:-23.435089180292, lng:-46.728342980292}}, location:{lat:-23.4337402, lng:-46.726994}, location_type:"ROOFTOP"}, place_id:"ChIJgVGzRqL7zpQRTQPNqsmBVLY", types:{"street_address"}}}, status:"OK"} I would like to extract just 02991-040 which appears three times on the returning message, once as part of the formatted address: formatted_address:\"Rua Dr. Carmelo D'Agostino, 628 - Jardim Rincão, São Paulo - SP, 02991-040, Brazil and twice as the postal code {short_name:"02991-040", long_name:"02991-040", types:{"postal_code"} I have spent quite some time trying to find it by myself but may be I haven't tried hard / smart enough so would really appreciate any help. Best
The script has three variables firstResult, secondResult and thirdResult which will contain the three values you want to extract. if Resultado's status is "OK" then set theData to item 1 of results of Resultado set formattedAddress to formatted_address of theData set {TID, text item delimiters} to {text item delimiters, ", "} set firstResult to text item 4 of formattedAddress set text item delimiters to TID set components to address_components of theData tell last item of components set secondResult to its long_name set thirdResult to its short_name end tell end if