Converting Twitter Stream JSON to R dataframe - json

I have a text file in JSON format that I downloaded using code from this link. It looks something like this:
"{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"'ello miss...?\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954853199872\",\"favorited\":false,\"geo\":null,\"source\":\"\u003Ca href=\\"http:\/\/www.tweetdeck.com\\" rel=\\"nofollow\\"\u003ETweetDeck\u003C\/a\u003E\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":false,\"lang\":\"en\",\"friends_count\":145,\"profile_sidebar_border_color\":\"181A1E\",\"location\":\"Chile\",\"profile_background_image_url_https\":\"https:\/\/si0.twimg.com\/images\/themes\/theme9\/bg.gif\",\"id_str\":\"76862348\",\"listed_count\":5,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\/\/si0.twimg.com\/profile_images\/1547896482\/egotuiter_normal.png\",\"description\":\"Geek. Amante de la m\u00fasica. Ateo. Nortino. Estudio Derecho en la U. de Chile. Nadie lee mi blog. Cobreloino. Afortunadamente ya no uso lentes :D\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"666666\",\"default_profile\":false,\"profile_background_image_url\":\"http:\/\/a0.twimg.com\/images\/themes\/theme9\/bg.gif\",\"followers_count\":181,\"is_translator\":false,\"time_zone\":\"Buenos Aires\",\"profile_link_color\":\"2FC2EF\",\"protected\":false,\"created_at\":\"Thu Sep 24 05:11:20 +0000 2009\",\"profile_background_color\":\"1A1B1F\",\"name\":\"Camilo Rojas\",\"default_profile_image\":false,\"contributors_enabled\":false,\"statuses_count\":36450,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":false,\"url\":\"http:\/\/unnombreingenioso.blogspot.com\",\"profile_image_url\":\"http:\/\/a0.twimg.com\/profile_images\/1547896482\/egotuiter_normal.png\",\"screen_name\":\"rojas_altazor\",\"id\":76862348,\"verified\":false,\"utc_offset\":-10800,\"favourites_count\":42,\"profile_sidebar_fill_color\":\"252429\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954853199872}
"
"{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"VALENTINA CORTANTE!\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954861580288\",\"favorited\":false,\"geo\":null,\"source\":\"web\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":false,\"lang\":\"es\",\"friends_count\":251,\"profile_sidebar_border_color\":\"CC3366\",\"location\":\"\",\"profile_background_image_url_https\":\"https:\/\/si0.twimg.com\/images\/themes\/theme11\/bg.gif\",\"id_str\":\"292675295\",\"listed_count\":0,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\/\/si0.twimg.com\/profile_images\/2494400748\/7zd2z67iclw88rc0q8sr_normal.jpeg\",\"description\":\"Soy hermosaaaa y valen me ama\u2665\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"362720\",\"default_profile\":false,\"profile_background_image_url\":\"http:\/\/a0.twimg.com\/images\/themes\/theme11\/bg.gif\",\"followers_count\":399,\"is_translator\":false,\"time_zone\":\"Santiago\",\"profile_link_color\":\"B40B43\",\"protected\":false,\"created_at\":\"Wed May 04 01:35:06 +0000 2011\",\"profile_background_color\":\"FF6699\",\"name\":\"Valen me amodora\u2020\",\"default_profile_image\":false,\"contributors_enabled\":false,\"statuses_count\":2643,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":true,\"url\":null,\"profile_image_url\":\"http:\/\/a0.twimg.com\/profile_images\/2494400748\/7zd2z67iclw88rc0q8sr_normal.jpeg\",\"screen_name\":\"CamiCelie\",\"id\":292675295,\"verified\":false,\"utc_offset\":-14400,\"favourites_count\":2,\"profile_sidebar_fill_color\":\"E5507E\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954861580288}
"
I'm curious how to extract individual pieces to an R data frame. For instance this would ideally go to a data frame like this:
screen_name text
rojas_altazor 'ello miss...?
CamiCelie VALENTINA CORTANTE!
... ...
This is my first foray into using JSON files so any suggestions would be appreciated.

First, using the rjson library, we pull in the stream - in this case, I'm using one user's timeline.
library(rjson)
timeline=fromJSON(file="http://api.twitter.com/1/statuses/user_timeline.json?screen_name=douglbutt&count=100")
This gives us a list - I always have trouble breaking hierarchical lists down into neat tables, so I'm going to do it field by field.
timelist=unlist(timeline)
tweets=timelist[names(timelist)=="text"]
names=timelist[names(timelist)=="user.screen_name"]
times=timelist[names(timelist)=="created_at"]
tweet.frame=data.frame(tweets,names,times)
So we should have them all in tweet.frame. If you want to look at the available fields in unlisted data, try:
unique(names(timelist))

twit<-c("{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"'ello miss...?\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954853199872\",\"favorited\":false,\"geo\":null,\"source\":\"\\u003Ca href=\\\"http:\\/\\/www.tweetdeck.com\\\" rel=\\\"nofollow\\\"\\u003ETweetDeck\\u003C\\/a\\u003E\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":false,\"lang\":\"en\",\"friends_count\":145,\"profile_sidebar_border_color\":\"181A1E\",\"location\":\"Chile\",\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/images\\/themes\\/theme9\\/bg.gif\",\"id_str\":\"76862348\",\"listed_count\":5,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/1547896482\\/egotuiter_normal.png\",\"description\":\"Geek. Amante de la m\\u00fasica. Ateo. Nortino. Estudio Derecho en la U. de Chile. Nadie lee mi blog. Cobreloino. Afortunadamente ya no uso lentes :D\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"666666\",\"default_profile\":false,\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/images\\/themes\\/theme9\\/bg.gif\",\"followers_count\":181,\"is_translator\":false,\"time_zone\":\"Buenos Aires\",\"profile_link_color\":\"2FC2EF\",\"protected\":false,\"created_at\":\"Thu Sep 24 05:11:20 +0000 2009\",\"profile_background_color\":\"1A1B1F\",\"name\":\"Camilo Rojas\",\"default_profile_image\":false,\"contributors_enabled\":false,\"statuses_count\":36450,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":false,\"url\":\"http:\\/\\/unnombreingenioso.blogspot.com\",\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/1547896482\\/egotuiter_normal.png\",\"screen_name\":\"rojas_altazor\",\"id\":76862348,\"verified\":false,\"utc_offset\":-10800,\"favourites_count\":42,\"profile_sidebar_fill_color\":\"252429\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954853199872}",
"", "{\"contributors\":null,\"coordinates\":null,\"in_reply_to_user_id\":null,\"truncated\":false,\"text\":\"VALENTINA CORTANTE!\",\"entities\":{\"hashtags\":[],\"urls\":[],\"user_mentions\":[]},\"place\":null,\"id_str\":\"234702954861580288\",\"favorited\":false,\"geo\":null,\"source\":\"web\",\"retweet_count\":0,\"in_reply_to_status_id_str\":null,\"in_reply_to_screen_name\":null,\"created_at\":\"Sun Aug 12 17:28:39 +0000 2012\",\"in_reply_to_user_id_str\":null,\"user\":{\"show_all_inline_media\":false,\"lang\":\"es\",\"friends_count\":251,\"profile_sidebar_border_color\":\"CC3366\",\"location\":\"\",\"profile_background_image_url_https\":\"https:\\/\\/si0.twimg.com\\/images\\/themes\\/theme11\\/bg.gif\",\"id_str\":\"292675295\",\"listed_count\":0,\"profile_use_background_image\":true,\"profile_image_url_https\":\"https:\\/\\/si0.twimg.com\\/profile_images\\/2494400748\\/7zd2z67iclw88rc0q8sr_normal.jpeg\",\"description\":\"Soy hermosaaaa y valen me ama\\u2665\",\"follow_request_sent\":null,\"following\":null,\"profile_text_color\":\"362720\",\"default_profile\":false,\"profile_background_image_url\":\"http:\\/\\/a0.twimg.com\\/images\\/themes\\/theme11\\/bg.gif\",\"followers_count\":399,\"is_translator\":false,\"time_zone\":\"Santiago\",\"profile_link_color\":\"B40B43\",\"protected\":false,\"created_at\":\"Wed May 04 01:35:06 +0000 2011\",\"profile_background_color\":\"FF6699\",\"name\":\"Valen me amodora\\u2020\",\"default_profile_image\":false,\"contributors_enabled\":false,\"statuses_count\":2643,\"geo_enabled\":false,\"notifications\":null,\"profile_background_tile\":true,\"url\":null,\"profile_image_url\":\"http:\\/\\/a0.twimg.com\\/profile_images\\/2494400748\\/7zd2z67iclw88rc0q8sr_normal.jpeg\",\"screen_name\":\"CamiCelie\",\"id\":292675295,\"verified\":false,\"utc_offset\":-14400,\"favourites_count\":2,\"profile_sidebar_fill_color\":\"E5507E\"},\"retweeted\":false,\"in_reply_to_status_id\":null,\"id\":234702954861580288}",
"")
library(RJSONIO)
twit1<-fromJSON(twit[1])
> twit1$user$screen_name
[1] "rojas_altazor"
> twit1$text
[1] "'ello miss...?"
> twit2<-fromJSON(twit[3])
> twit2$user$screen_name
[1] "CamiCelie"
> twit2$text
[1] "VALENTINA CORTANTE!"

Related

Is there a way to extract part of a dataframe using pandas (or other library) in Python?

Hi I am new on Python and pandas. So please excuse my mistakes.
I want to know if it's possible to use a dataframe and sort it with pandas.
I'm still in class, but I'd like to advance in my projects.
I want / I'd like to extract part of the JSON file.
I want all the system_name =Métro de Paris to be in a JSON file.
Following are the current code I have (JSON and script).
Thanks in advance for your help!
import pandas as pd
# parse according to "system_name":"Métro de Paris"
data = pd.read_json(r'paris_lines_systems_and_modes.json')
[{"id":406,"name":"Ancien M 2nord","url_name":"406-ancien-m-2nord","color":"#000","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":405,"name":"Ancien M 2sud","url_name":"405-ancien-m-2sud","color":"#000","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":399,"name":"Ancien M 14","url_name":"399-ancien-m-14","color":"#000","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":460,"name":"Ancienne ligne A Nord-Sud","url_name":"460-ancienne-ligne-a-nord-sud","color":"#000","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":459,"name":"Ancienne ligne B Nord-Sud","url_name":"459-ancienne-ligne-b-nord-sud","color":"#000","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":411,"name":"Austerlitz-Auteuil","url_name":"411-austerlitz-auteuil","color":"#000","system_id":303,"system_name":"Navettes fluviales","transport_mode_id":0,"transport_mode_name":"default"},{"id":471,"name":"CDG Express","url_name":"471-cdgexpress","color":"#f0871f","system_id":299,"system_name":"Dessertes aéroportuaires","transport_mode_id":7,"transport_mode_name":"people_mover"},{"id":465,"name":"CDGVal 1","url_name":"465-cdgval-1","color":"#0a3071","system_id":299,"system_name":"Dessertes aéroportuaires","transport_mode_id":7,"transport_mode_name":"people_mover"},{"id":466,"name":"CDGVal 2","url_name":"466-cdgval-2","color":"#0a3071","system_id":299,"system_name":"Dessertes aéroportuaires","transport_mode_id":7,"transport_mode_name":"people_mover"},{"id":410,"name":"Charenton-Auteuil","url_name":"410-charenton-auteuil","color":"#000","system_id":303,"system_name":"Navettes fluviales","transport_mode_id":0,"transport_mode_name":"default"},{"id":407,"name":"Funiculaire de Monmartre","url_name":"407-funiculaire","color":"#601e92","system_id":302,"system_name":"Funiculaire","transport_mode_id":0,"transport_mode_name":"default"},{"id":381,"name":"H","url_name":"381-h","color":"#7b4339","system_id":298,"system_name":"Transilien","transport_mode_id":2,"transport_mode_name":"inter_city_rail"},{"id":382,"name":"J","url_name":"382-j","color":"#cdcd00","system_id":298,"system_name":"Transilien","transport_mode_id":2,"transport_mode_name":"inter_city_rail"},{"id":383,"name":"K","url_name":"383-k","color":"#c7b300","system_id":298,"system_name":"Transilien","transport_mode_id":2,"transport_mode_name":"inter_city_rail"},{"id":384,"name":"L","url_name":"384-l","color":"#7584bc","system_id":298,"system_name":"Transilien","transport_mode_id":2,"transport_mode_name":"inter_city_rail"},{"id":472,"name":"Le Bus Direct 1","url_name":"472-le-bus-direct-1","color":"#b20066","system_id":299,"system_name":"Dessertes aéroportuaires","transport_mode_id":8,"transport_mode_name":"bus"},{"id":473,"name":"Le Bus Direct 2","url_name":"473-le-bus-direct-2","color":"#7bbb58","system_id":299,"system_name":"Dessertes aéroportuaires","transport_mode_id":8,"transport_mode_name":"bus"},{"id":474,"name":"Le Bus Direct 3","url_name":"474-le-bus-direct-3","color":"#0098d8","system_id":299,"system_name":"Dessertes aéroportuaires","transport_mode_id":8,"transport_mode_name":"bus"},{"id":475,"name":"Le Bus Direct 4","url_name":"475-le-bus-direct-4","color":"#ff9b00","system_id":299,"system_name":"Dessertes aéroportuaires","transport_mode_id":8,"transport_mode_name":"bus"},{"id":412,"name":" Le Louvre-Ablon","url_name":"412-le-louvre-ablon","color":"#000","system_id":303,"system_name":"Navettes fluviales","transport_mode_id":0,"transport_mode_name":"default"},{"id":643,"name":"Ligne Horizon","url_name":"643-ligne-horizon","color":"#0092e2","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":352,"name":"M 1","url_name":"352-ligne-1","color":"#ffcd00","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":353,"name":"M 2","url_name":"353-ligne-2","color":"#006db8","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":354,"name":"M 3","url_name":"354-ligne-3","color":"#9b993b","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":355,"name":"M 3bis","url_name":"355-ligne-3bis","color":"#88d3df","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":356,"name":"M 4","url_name":"356-ligne-4","color":"#ba4a9a","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":357,"name":"M 5","url_name":"357-ligne-5","color":"#f78e4b","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":358,"name":"M 6","url_name":"358-ligne-6","color":"#77c596","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":303,"name":"M 7","url_name":"303-ligne-7","color":"#f49eb2","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":359,"name":"M 7bis","url_name":"359-ligne-7bis","color":"#77c596","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":360,"name":"M 8","url_name":"360-ligne-8","color":"#c5a3cc","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":361,"name":"M 9","url_name":"361-ligne-9","color":"#cdc92b","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":362,"name":"M 10","url_name":"362-ligne-10","color":"#dfb03a","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":363,"name":"M 11","url_name":"363-ligne-11","color":"#8c6539","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":364,"name":"M 12","url_name":"364-ligne-12","color":"#008c59","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":365,"name":"M 13","url_name":"365-ligne-13","color":"#87d2df","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":366,"name":"M 14","url_name":"366-ligne-14","color":"#662c91","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":395,"name":"M 15","url_name":"395-m-15","color":"#a60f31","system_id":300,"system_name":"Grand Paris Express","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":396,"name":"M 16","url_name":"396-m-16","color":"#ec7cae","system_id":300,"system_name":"Grand Paris Express","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":397,"name":"M 17","url_name":"397-m-17","color":"#ec7cae","system_id":300,"system_name":"Grand Paris Express","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":398,"name":"M 18","url_name":"398-m-18","color":"#95bf32","system_id":300,"system_name":"Grand Paris Express","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":385,"name":"N","url_name":"385-n","color":"#00a092","system_id":298,"system_name":"Transilien","transport_mode_id":2,"transport_mode_name":"inter_city_rail"},{"id":476,"name":"Navette","url_name":"476-navette","color":"#000","system_id":83,"system_name":"Métro de Paris","transport_mode_id":4,"transport_mode_name":"heavy_rail"},{"id":394,"name":"Orlyval","url_name":"394-orlyval","color":"#7d7e7e","system_id":299,"system_name":"Dessertes aéroportuaires","transport_mode_id":7,"transport_mode_name":"people_mover"},{"id":386,"name":"P","url_name":"386-p","color":"#f0b600","system_id":298,"system_name":"Transilien","transport_mode_id":2,"transport_mode_name":"inter_city_rail"},{"id":387,"name":"R","url_name":"387-r","color":"#e4b4d1","system_id":298,"system_name":"Transilien","transport_mode_id":2,"transport_mode_name":"inter_city_rail"},{"id":376,"name":"RER A","url_name":"376-rer-a","color":"#ec1b2a","system_id":297,"system_name":"RER","transport_mode_id":3,"transport_mode_name":"commuter_rail"},{"id":377,"name":"RER B","url_name":"377-rer-b","color":"#4c90cc","system_id":297,"system_name":"RER","transport_mode_id":3,"transport_mode_name":"commuter_rail"},{"id":378,"name":"RER C","url_name":"378-rer-c","color":"#ffdc1d","system_id":297,"system_name":"RER","transport_mode_id":3,"transport_mode_name":"commuter_rail"},{"id":379,"name":"RER D","url_name":"379-rer-d","color":"#00ab66","system_id":297,"system_name":"RER","transport_mode_id":3,"transport_mode_name":"commuter_rail"},{"id":380,"name":"RER E","url_name":"380-rer-e","color":"#c67db4","system_id":297,"system_name":"RER","transport_mode_id":3,"transport_mode_name":"commuter_rail"},{"id":414,"name":"STCRP","url_name":"414-stcrp","color":"#000","system_id":303,"system_name":"Navettes fluviales","transport_mode_id":0,"transport_mode_name":"default"},{"id":367,"name":"T 1","url_name":"367-ligne-1","color":"#14609e","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":368,"name":"T 2","url_name":"368-ligne-2","color":"#ba498d","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":369,"name":"T 3a","url_name":"369-ligne-3a","color":"#ef7d2d","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":370,"name":"T 3b","url_name":"370-ligne-3b","color":"#009b41","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":371,"name":"T 4","url_name":"371-ligne-4","color":"#d9922b","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":372,"name":"T 5","url_name":"372-ligne-5","color":"#914e8e","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":373,"name":"T 6","url_name":"373-ligne-7","color":"#e95c36","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":375,"name":"T 7","url_name":"375-t-7","color":"#885f30","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":374,"name":"T 8","url_name":"374-t-8","color":"#8c8235","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":389,"name":"T 9","url_name":"389-t-9","color":"#009cb4","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":390,"name":"T 10","url_name":"390-t-10","color":"#8c8235","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":391,"name":"T 11","url_name":"391-t-11","color":"#df5f3d","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":506,"name":"T 11 express","url_name":"506-t-11-express","color":"#e2562e","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":392,"name":"T 12","url_name":"392-t-12","color":"#a02a3a","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":393,"name":"T 13","url_name":"393-t-13","color":"#885f30","system_id":296,"system_name":"Tramway d'Île-de-France","transport_mode_id":5,"transport_mode_name":"light_rail"},{"id":400,"name":"T Zen 1","url_name":"400-t-zen-1","color":"#c6cd01","system_id":301,"system_name":"TZen","transport_mode_id":6,"transport_mode_name":"brt"},{"id":401,"name":"T Zen 2","url_name":"401-t-zen-2","color":"#c6cd01","system_id":301,"system_name":"TZen","transport_mode_id":6,"transport_mode_name":"brt"},{"id":402,"name":"T Zen 3","url_name":"402-t-zen-3","color":"#c6cd01","system_id":301,"system_name":"TZen","transport_mode_id":6,"transport_mode_name":"brt"},{"id":403,"name":"T Zen 4","url_name":"403-t-zen-4","color":"#c6cd01","system_id":301,"system_name":"TZen","transport_mode_id":6,"transport_mode_name":"brt"},{"id":404,"name":"T Zen 5","url_name":"404-t-zen-5","color":"#c6cd01","system_id":301,"system_name":"TZen","transport_mode_id":6,"transport_mode_name":"brt"},{"id":409,"name":"Tuileries-Suresnes","url_name":"409-tuileries-suresnes","color":"#000","system_id":303,"system_name":"Navettes fluviales","transport_mode_id":0,"transport_mode_name":"default"},{"id":388,"name":"U","url_name":"388-u","color":"#d60058","system_id":298,"system_name":"Transilien","transport_mode_id":2,"transport_mode_name":"inter_city_rail"},{"id":408,"name":"Voguéo","url_name":"408-vogueo","color":"#99d420","system_id":303,"system_name":"Navettes fluviales","transport_mode_id":0,"transport_mode_name":"default"}]
So I managed to find how do extract part of the data you want to use / analyse.
You should simply call the column you are trying to extract (in my case system_name) and give its value (in my case "Métro de Paris"). Here The dataframe, data, will now have the data with value "Métro de Paris" form the column "system_name".
data = data[data.system_name == "Métro de Paris"]

issue parsing Twitter JSON in Google App Script

I'm using the code described in the GAS documentation to retrieve my Twitter timeline.
The lines
var response = twitterService.fetch('https://api.twitter.com/1.1/statuses/home_timeline.json?count=1');
response=response.getContentText();
yield the following result:
[{"created_at":"Sun Jan 14 15:30:00 +0000 2018","id":952563458616172544,"id_str":"952563458616172544","text":"#UltimOra, \u201cAi piedi le Clarks\u201d; #LiberieUguali trova il primo punto di convergenza fra i vari leader https:\/\/omissis","truncated":false,"entities":{"hashtags":[{"text":"UltimOra","indices":[0,9]},{"text":"LiberieUguali","indices":[33,47]}],"symbols":[],"user_mentions":[],"urls":[{"url":"https:\/\/omissis","expanded_url":"http:\/\/www.lercio.it\/ai-piedi-le-clarks-liberi-e-uguali-trova-il-primo-punto-di-convergenza-fra-i-vari-leader\/","display_url":"lercio.it\/ai-piedi-le-cl\u2026","indices":[102,125]}]},"source":"\u003ca href=\"https:\/\/about.twitter.com\/products\/tweetdeck\" rel=\"nofollow\"\u003eTweetDeck\u003c\/a\u003e","in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":910827588,"id_str":"910827588","name":"lercio-notizie","screen_name":"lercionotizie","location":"","description":"Dal 13 aprile 2017 in fumetteria e libreria con \"Lercio, lo sporco che fa notizia. Il libro\" (Shockdom)","url":"http:\/\/omissis","entities":{"url":{"urls":[{"url":"http:\/\/omissis","expanded_url":"http:\/\/www.lercio.it","display_url":"lercio.it","indices":[0,22]}]},"description":{"urls":[]}},"protected":false,"followers_count":491614,"friends_count":25,"listed_count":712,"created_at":"Sun Oct 28 18:19:33 +0000 2012","favourites_count":385,"utc_offset":3600,"time_zone":"Amsterdam","geo_enabled":false,"verified":false,"statuses_count":7335,"lang":"it","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"FFFFFF","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/696203701\/001bc779080491195bc16054af629016.png","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/696203701\/001bc779080491195bc16054af629016.png","profile_background_tile":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/2932719853\/4bcd23b39293870bf1832aec72b6fe3d_normal.png","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/2932719853\/4bcd23b39293870bf1832aec72b6fe3d_normal.png","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/910827588\/1467828171","profile_link_color":"9B1212","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":true,"follow_request_sent":false,"notifications":false,"translator_type":"none"},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":73,"favorite_count":186,"favorited":false,"retweeted":false,"possibly_sensitive":false,"possibly_sensitive_appealable":false,"lang":"it"}]
But when I add the code line
response=JSON.parse(response);
the JSON file is totally altered, it doesn't even look a JSON file and its content becomes:
{possibly_sensitive_appealable=false, in_reply_to_status_id_str=null, in_reply_to_status_id=null, created_at=Sun Jan 14 15:30:00 +0000 2018, in_reply_to_user_id_str=null, source=TweetDeck, retweet_count=73, retweeted=false, geo=null, in_reply_to_screen_name=null, is_quote_status=false, id_str=952563458616172544, in_reply_to_user_id=null, favorite_count=186, id=9.5256345861617254E17, text=#UltimOra, “Ai piedi le Clarks”; #LiberieUguali trova il primo punto di convergenza fra i vari leader https://omissis, place=null, lang=it, favorited=false, possibly_sensitive=false, coordinates=null, truncated=false, entities={urls=[Ljava.lang.Object;#7d41de2d, hashtags=[Ljava.lang.Object;#5e05507d, user_mentions=[Ljava.lang.Object;#7b5572f2, symbols=[Ljava.lang.Object;#fb373c9}, contributors=null, user={utc_offset=3600, friends_count=25, profile_image_url_https=https://pbs.twimg.com/profile_images/2932719853/4bcd23b39293870bf1832aec72b6fe3d_normal.png, listed_count=712, profile_background_image_url=http://pbs.twimg.com/profile_background_images/696203701/001bc779080491195bc16054af629016.png, default_profile_image=false, favourites_count=385, description=Dal 13 aprile 2017 in fumetteria e libreria con "Lercio, lo sporco che fa notizia. Il libro" (Shockdom), created_at=Sun Oct 28 18:19:33 +0000 2012, is_translator=false, profile_background_image_url_https=https://pbs.twimg.com/profile_background_images/696203701/001bc779080491195bc16054af629016.png, protected=false, screen_name=lercionotizie, id_str=910827588, profile_link_color=9B1212, is_translation_enabled=false, translator_type=none, id=910827588, geo_enabled=false, profile_background_color=FFFFFF, lang=it, has_extended_profile=false, profile_sidebar_border_color=000000, profile_text_color=333333, verified=false, profile_image_url=http://pbs.twimg.com/profile_images/2932719853/4bcd23b39293870bf1832aec72b6fe3d_normal.png, time_zone=Amsterdam, url=http://omissis, contributors_enabled=false, profile_background_tile=false, profile_banner_url=https://pbs.twimg.com/profile_banners/910827588/1467828171, entities={description={urls=[Ljava.lang.Object;#7c7074a4}, url={urls=[Ljava.lang.Object;#80245ef}}, statuses_count=7335, follow_request_sent=false, followers_count=491614, profile_use_background_image=true, default_profile=false, following=true, name=lercio-notizie, location=, profile_sidebar_fill_color=DDEEF6, notifications=false}}
What's wrong?
You are probably using Logger.log for checking results of code execution.
As noted in documentation:
The method Logger.log expects a string value, but if you pass in an object or array it will do its best to convert that into a string.
So response in this case response=response.getContentText() will give you String with expected behaviour when used in Logger.log, but in case response=JSON.parse(response) will give Object, that will have output in Logger.log in format [key1=val1, key2=val2, ..].
You can get used to it, or you can use convertion of your object to string before logging it:
Logger.log(JSON.stringify(response))

Splitting strings by words in Scala Spark

What I'm trying to do here is to leave texts only from each tweet.
import org.apache.spark.{SparkConf, SparkContext}
import scala.io.Source
object shortTwitter {
def main(args: Array[String]): Unit = {
val sparkConf = new SparkConf().setAppName("ShortTwitterAnalysis").setMaster("local[2]")
val sc = new SparkContext(sparkConf)
val text = sc.textFile("/home/tobby/data/shortTwitter.txt")
val counts = text
.map(_.toLowerCase)
.map(_.toString)
.map(_.replace("\t", ""))
.map(_.replace("\"", ""))
.map(_.replace("\n", ""))
.map(_.replaceAll("[\\p{C}]", ""))
.map(_.split("\"text\":\"")(1).split("\",\"source\":")(0))
counts.foreach(println)
}
}
But the last map function .map(_.split("\"text\":\"")(1).split("\",\"source\":")(0)) does not work. Do you have any advice?
Without the .map(_.split("\"text\":\"")(1).split("\",\"source\":")(0)) my tweets look like below :
{created_at:wed jul 16 23:58:19 +0000 2014,id:489559687189110784,id_str:489559687189110784,text:a rose by any other name would smell as sweet,source:\u003ca href=\https:\/\/twitter.com\/download\/android\ rel=\nofollow\\u003etwitter for android\u003c\/a\u003e,truncated:false,in_reply_to_status_id:null,in_reply_to_status_id_str:null,in_reply_to_user_id:null,in_reply_to_user_id_str:null,in_reply_to_screen_name:null,user:{id:621244372,id_str:621244372,name:\u2665,screen_name:ivunia_ontrinae,location:,url:null,description:me myself & i \u2764,protected:false,verified:false,followers_count:1023,friends_count:591,listed_count:1,favourites_count:1909,statuses_count:26770,created_at:thu jun 28 19:23:06 +0000 2012,utc_offset:-10800,time_zone:atlantic time (canada),geo_enabled:true,lang:en,contributors_enabled:false,is_translator:false,profile_background_color:c0deed,profile_background_image_url:http:\/\/pbs.twimg.com\/profile_background_images\/378800000101658269\/ec0820565f0451a3ce7169c776fbe41f.jpeg,profile_background_image_url_https:https:\/\/pbs.twimg.com\/profile_background_images\/378800000101658269\/ec0820565f0451a3ce7169c776fbe41f.jpeg,profile_background_tile:true,profile_link_color:e62bb4,profile_sidebar_border_color:000000,profile_sidebar_fill_color:ddeef6,profile_text_color:333333,profile_use_background_image:true,profile_image_url:http:\/\/pbs.twimg.com\/profile_images\/483373612749959168\/f3qpy_66_normal.jpeg,profile_image_url_https:https:\/\/pbs.twimg.com\/profile_images\/483373612749959168\/f3qpy_66_normal.jpeg,profile_banner_url:https:\/\/pbs.twimg.com\/profile_banners\/621244372\/1404758956,default_profile:false,default_profile_image:false,following:null,follow_request_sent:null,notifications:null},geo:null,coordinates:null,place:null,contributors:null,retweet_count:0,favorite_count:0,entities:{hashtags:[],trends:[],urls:[],user_mentions:[],symbols:[]},favorited:false,retweeted:false,possibly_sensitive:false,filter_level:medium,lang:en}
{created_at:wed jul 16 23:58:19 +0000 2014,id:489559687189110784,id_str:489559687189110784,text:a rose is a rose is a rose,source:\u003ca href=\https:\/\/twitter.com\/download\/android\ rel=\nofollow\\u003etwitter for android\u003c\/a\u003e,truncated:false,in_reply_to_status_id:null,in_reply_to_status_id_str:null,in_reply_to_user_id:null,in_reply_to_user_id_str:null,in_reply_to_screen_name:null,user:{id:621244372,id_str:621244372,name:\u2665,screen_name:ivunia_ontrinae,location:,url:null,description:me myself & i \u2764,protected:false,verified:false,followers_count:1023,friends_count:591,listed_count:1,favourites_count:1909,statuses_count:26770,created_at:thu jun 28 19:23:06 +0000 2012,utc_offset:-10800,time_zone:atlantic time (canada),geo_enabled:true,lang:en,contributors_enabled:false,is_translator:false,profile_background_color:c0deed,profile_background_image_url:http:\/\/pbs.twimg.com\/profile_background_images\/378800000101658269\/ec0820565f0451a3ce7169c776fbe41f.jpeg,profile_background_image_url_https:https:\/\/pbs.twimg.com\/profile_background_images\/378800000101658269\/ec0820565f0451a3ce7169c776fbe41f.jpeg,profile_background_tile:true,profile_link_color:e62bb4,profile_sidebar_border_color:000000,profile_sidebar_fill_color:ddeef6,profile_text_color:333333,profile_use_background_image:true,profile_image_url:http:\/\/pbs.twimg.com\/profile_images\/483373612749959168\/f3qpy_66_normal.jpeg,profile_image_url_https:https:\/\/pbs.twimg.com\/profile_images\/483373612749959168\/f3qpy_66_normal.jpeg,profile_banner_url:https:\/\/pbs.twimg.com\/profile_banners\/621244372\/1404758956,default_profile:false,default_profile_image:false,following:null,follow_request_sent:null,notifications:null},geo:null,coordinates:null,place:null,contributors:null,retweet_count:0,favorite_count:0,entities:{hashtags:[],trends:[],urls:[],user_mentions:[],symbols:[]},favorited:false,retweeted:false,possibly_sensitive:false,filter_level:medium,lang:en}
{created_at:wed jul 16 23:58:19 +0000 2014,id:489559687176945664,id_str:489559687176945664,text:love is like a rose the joy of all the earth,source:\u003ca href=\http:\/\/twitter.com\/download\/iphone\ rel=\nofollow\\u003etwitter for iphone\u003c\/a\u003e,truncated:false,in_reply_to_status_id:null,in_reply_to_status_id_str:null,in_reply_to_user_id:null,in_reply_to_user_id_str:null,in_reply_to_screen_name:null,user:{id:363819213,id_str:363819213,name:ivanna010394,screen_name:ivannacarrillo,location:,url:null,description:null,protected:false,verified:false,followers_count:243,friends_count:530,listed_count:0,favourites_count:26,statuses_count:5672,created_at:sun aug 28 18:58:49 +0000 2011,utc_offset:-14400,time_zone:eastern time (us & canada),geo_enabled:false,lang:es,contributors_enabled:false,is_translator:false,profile_background_color:642d8b,profile_background_image_url:http:\/\/pbs.twimg.com\/profile_background_images\/767201253\/661eb2d4915e9ee6566647dcbaab0186.jpeg,profile_background_image_url_https:https:\/\/pbs.twimg.com\/profile_background_images\/767201253\/661eb2d4915e9ee6566647dcbaab0186.jpeg,profile_background_tile:true,profile_link_color:ff0000,profile_sidebar_border_color:ffffff,profile_sidebar_fill_color:7ac3ee,profile_text_color:3d1957,profile_use_background_image:true,profile_image_url:http:\/\/pbs.twimg.com\/profile_images\/455873054703648768\/_b4mf6o7_normal.jpeg,profile_image_url_https:https:\/\/pbs.twimg.com\/profile_images\/455873054703648768\/_b4mf6o7_normal.jpeg,profile_banner_url:https:\/\/pbs.twimg.com\/profile_banners\/363819213\/1402261141,default_profile:false,default_profile_image:false,following:null,follow_request_sent:null,notifications:null},geo:null,coordinates:null,place:null,contributors:null,retweeted_status:{created_at:wed jul 16 13:45:28 +0000 2014,id:489405458168709120,id_str:489405458168709120,text:our milan show is now sold out, thankyou :d tickets are still available for most of europe ! http:\/\/t.co\/arnh7pvoap http:\/\/t.co\/t5wzyocrtu,source:\u003ca href=\http:\/\/twitter.com\ rel=\nofollow\\u003etwitter web client\u003c\/a\u003e,truncated:false,in_reply_to_status_id:null,in_reply_to_status_id_str:null,in_reply_to_user_id:null,in_reply_to_user_id_str:null,in_reply_to_screen_name:null,user:{id:264107729,id_str:264107729,name:5 seconds of summer,screen_name:5sos,location:sydney, australia,url:http:\/\/www.facebook.com\/5secondsofsummer,description:4 aussies making music :) love the people who support us! our album is out :) http:\/\/po.st\/or93y4 | #ashton5sos #calum5sos #michael5sos #luke5sos,protected:false,verified:true,followers_count:3704204,friends_count:28660,listed_count:20024,favourites_count:1061,statuses_count:17297,created_at:fri mar 11 10:18:46 +0000 2011,utc_offset:36000,time_zone:sydney,geo_enabled:false,lang:en,contributors_enabled:false,is_translator:false,profile_background_color:000000,profile_background_image_url:http:\/\/pbs.twimg.com\/profile_background_images\/483531430371147778\/0gzkh2zi.jpeg,profile_background_image_url_https:https:\/\/pbs.twimg.com\/profile_background_images\/483531430371147778\/0gzkh2zi.jpeg,profile_background_tile:false,profile_link_color:c21b1b,profile_sidebar_border_color:ffffff,profile_sidebar_fill_color:ddeef6,profile_text_color:333333,profile_use_background_image:true,profile_image_url:http:\/\/pbs.twimg.com\/profile_images\/485730748574752768\/zm1ctcvv_normal.jpeg,profile_image_url_https:https:\/\/pbs.twimg.com\/profile_images\/485730748574752768\/zm1ctcvv_normal.jpeg,profile_banner_url:https:\/\/pbs.twimg.com\/profile_banners\/264107729\/1404117825,default_profile:false,default_profile_image:false,following:null,follow_request_sent:null,notifications:null},geo:null,coordinates:null,place:null,contributors:null,retweet_count:12648,favorite_count:31390,entities:{hashtags:[],trends:[],urls:[{url:http:\/\/t.co\/arnh7pvoap,expanded_url:http:\/\/5sos.com\/live,display_url:5sos.com\/live,indices:[93,115]}],user_mentions:[],symbols:[],media:[{id:489405457111715840,id_str:489405457111715840,indices:[116,138],media_url:http:\/\/pbs.twimg.com\/media\/bsq3q5zieaakbgg.jpg,media_url_https:https:\/\/pbs.twimg.com\/media\/bsq3q5zieaakbgg.jpg,url:http:\/\/t.co\/t5wzyocrtu,display_url:pic.twitter.com\/t5wzyocrtu,expanded_url:http:\/\/twitter.com\/5sos\/status\/489405458168709120\/photo\/1,type:photo,sizes:{small:{w:340,h:613,resize:fit},thumb:{w:150,h:150,resize:crop},medium:{w:600,h:1081,resize:fit},large:{w:811,h:1461,resize:fit}}}]},favorited:false,retweeted:false,possibly_sensitive:false,filter_level:low,lang:en},retweet_count:0,favorite_count:0,entities:{hashtags:[],trends:[],urls:[{url:http:\/\/t.co\/arnh7pvoap,expanded_url:http:\/\/5sos.com\/live,display_url:5sos.com\/live,indices:[103,125]}],user_mentions:[{screen_name:5sos,name:5 seconds of summer,id:264107729,id_str:264107729,indices:[3,8]}],symbols:[],media:[{id:489405457111715840,id_str:489405457111715840,indices:[126,140],media_url:http:\/\/pbs.twimg.com\/media\/bsq3q5zieaakbgg.jpg,media_url_https:https:\/\/pbs.twimg.com\/media\/bsq3q5zieaakbgg.jpg,url:http:\/\/t.co\/t5wzyocrtu,display_url:pic.twitter.com\/t5wzyocrtu,expanded_url:http:\/\/twitter.com\/5sos\/status\/489405458168709120\/photo\/1,type:photo,sizes:{small:{w:340,h:613,resize:fit},thumb:{w:150,h:150,resize:crop},medium:{w:600,h:1081,resize:fit},large:{w:811,h:1461,resize:fit}},source_status_id:489405458168709120,source_status_id_str:489405458168709120}]},favorited:false,retweeted:false,possibly_sensitive:false,filter_level:medium,lang:en}
{created_at:sat jan 16 12:00:47 +0000 2016,id:688330052233199616,id_str:688330052233199616,text:rt #nba2k: the battle of two young teams. tough season but one will emerge victorious. who will it be? lakers or 76ers? https:\/\/t.co\/nukkjq\u2026,source:\u003ca href=\http:\/\/twitter.com\ rel=\nofollow\\u003etwitter web client\u003c\/a\u003e,truncated:false,in_reply_to_status_id:null,in_reply_to_status_id_str:null,in_reply_to_user_id:null,in_reply_to_user_id_str:null,in_reply_to_screen_name:null,user:{id:4817727209,id_str:4817727209,name:mark lieyg,screen_name:_yungwiggins_,location:null,url:null,description:null,protected:false,verified:false,followers_count:3,friends_count:40,listed_count:0,favourites_count:0,statuses_count:39,created_at:sat jan 16 11:06:38 +0000 2016,utc_offset:-28800,time_zone:pacific time (us & canada),geo_enabled:false,lang:en,contributors_enabled:false,is_translator:false,profile_background_color:f5f8fa,profile_background_image_url:,profile_background_image_url_https:,profile_background_tile:false,profile_link_color:2b7bb9,profile_sidebar_border_color:c0deed,profile_sidebar_fill_color:ddeef6,profile_text_color:333333,profile_use_background_image:true,profile_image_url:http:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_1_normal.png,profile_image_url_https:https:\/\/abs.twimg.com\/sticky\/default_profile_images\/default_profile_1_normal.png,default_profile:true,default_profile_image:true,following:null,follow_request_sent:null,notifications:null},geo:null,coordinates:null,place:null,contributors:null,retweeted_status: {created_at:sat jan 02 03:31:10 +0000 2016,id:683128371627200513,id_str:683128371627200513,text:the battle of two young teams. tough season but one will emerge victorious. who will it be? lakers or 76ers? https:\/\/t.co\/nukkjqqspa,source:\u003ca href=\http:\/\/percolate.com\ rel=\nofollow\\u003epercolate\u003c\/a\u003e,truncated:false,in_reply_to_status_id:null,in_reply_to_status_id_str:null,in_reply_to_user_id:null,in_reply_to_user_id_str:null,in_reply_to_screen_name:null,user:{id:15573174,id_str:15573174,name:nba 2k 2k16,screen_name:nba2k,location:novato, ca,url:http:\/\/www.2k.com,description:esrb rating: everyone 10+. #nba2k16 available now for playstation 4 & xbox one, playstation 3 & xbox 360 & pc http:\/\/2kgam.es\/buynba2k16,protected:false,verified:true,followers_count:948071,friends_count:1630,listed_count:3305,favourites_count:10,statuses_count:8162,created_at:wed jul 23 21:57:14 +0000 2008,utc_offset:-28800,time_zone:pacific time (us & canada),geo_enabled:true,lang:en,contributors_enabled:false,is_translator:false,profile_background_color:000000,profile_background_image_url:http:\/\/pbs.twimg.com\/profile_background_images\/539865904528371712\/gnb-ggrq.png,profile_background_image_url_https:https:\/\/pbs.twimg.com\/profile_background_images\/539865904528371712\/gnb-ggrq.png,profile_background_tile:false,profile_link_color:ff0300,profile_sidebar_border_color:ffffff,profile_sidebar_fill_color:0d2b44,profile_text_color:408af2,profile_use_background_image:true,profile_image_url:http:\/\/pbs.twimg.com\/profile_images\/606562975109890048\/sumjozun_normal.jpg,profile_image_url_https:https:\/\/pbs.twimg.com\/profile_images\/606562975109890048\/sumjozun_normal.jpg,profile_banner_url:https:\/\/pbs.twimg.com\/profile_banners\/15573174\/1433457451,default_profile:false,default_profile_image:false,following:null,follow_request_sent:null,notifications:null},geo:null,coordinates:null,place:null,contributors:null,is_quote_status:false,retweet_count:112,favorite_count:547,entities:{hashtags:[],urls:[],user_mentions:[],symbols:[],media:[{id:683128370796736512,id_str:683128370796736512,indices:[109,132],media_url:http:\/\/pbs.twimg.com\/media\/cxr1okvusaamnu4.jpg,media_url_https:https:\/\/pbs.twimg.com\/media\/cxr1okvusaamnu4.jpg,url:https:\/\/t.co\/nukkjqqspa,display_url:pic.twitter.com\/nukkjqqspa,expanded_url:http:\/\/twitter.com\/nba2k\/status\/683128371627200513\/photo\/1,type:photo,sizes:{large:{w:1024,h:419,resize:fit},thumb:{w:150,h:150,resize:crop},medium:{w:600,h:245,resize:fit},small:{w:340,h:139,resize:fit}}}]},extended_entities:{media:[{id:683128370796736512,id_str:683128370796736512,indices:[109,132],media_url:http:\/\/pbs.twimg.com\/media\/cxr1okvusaamnu4.jpg,media_url_https:https:\/\/pbs.twimg.com\/media\/cxr1okvusaamnu4.jpg,url:https:\/\/t.co\/nukkjqqspa,display_url:pic.twitter.com\/nukkjqqspa,expanded_url:http:\/\/twitter.com\/nba2k\/status\/683128371627200513\/photo\/1,type:photo,sizes:{large:{w:1024,h:419,resize:fit},thumb:{w:150,h:150,resize:crop},medium:{w:600,h:245,resize:fit},small:{w:340,h:139,resize:fit}}}]},favorited:false,retweeted:false,possibly_sensitive:false,filter_level:low,lang:en},is_quote_status:false,retweet_count:0,favorite_count:0,entities:{hashtags:[],urls:[],user_mentions:[{screen_name:nba2k,name:nba 2k 2k16,id:15573174,id_str:15573174,indices:[3,9]}],symbols:[],media:[{id:683128370796736512,id_str:683128370796736512,indices:[120,140],media_url:http:\/\/pbs.twimg.com\/media\/cxr1okvusaamnu4.jpg,media_url_https:https:\/\/pbs.twimg.com\/media\/cxr1okvusaamnu4.jpg,url:https:\/\/t.co\/nukkjqqspa,display_url:pic.twitter.com\/nukkjqqspa,expanded_url:http:\/\/twitter.com\/nba2k\/status\/683128371627200513\/photo\/1,type:photo,sizes:{large:{w:1024,h:419,resize:fit},thumb:{w:150,h:150,resize:crop},medium:{w:600,h:245,resize:fit},small:{w:340,h:139,resize:fit}},source_status_id:683128371627200513,source_status_id_str:683128371627200513,source_user_id:15573174,source_user_id_str:15573174}]},extended_entities:{media:[{id:683128370796736512,id_str:683128370796736512,indices:[120,140],media_url:http:\/\/pbs.twimg.com\/media\/cxr1okvusaamnu4.jpg,media_url_https:https:\/\/pbs.twimg.com\/media\/cxr1okvusaamnu4.jpg,url:https:\/\/t.co\/nukkjqqspa,display_url:pic.twitter.com\/nukkjqqspa,expanded_url:http:\/\/twitter.com\/nba2k\/status\/683128371627200513\/photo\/1,type:photo,sizes:{large:{w:1024,h:419,resize:fit},thumb:{w:150,h:150,resize:crop},medium:{w:600,h:245,resize:fit},small:{w:340,h:139,resize:fit}},source_status_id:683128371627200513,source_status_id_str:683128371627200513,source_user_id:15573174,source_user_id_str:15573174}]},favorited:false,retweeted:false,possibly_sensitive:false,filter_level:low,lang:en,timestamp_ms:1452945647663}
{created_at:wed jul 16 23:58:19 +0000 2014,id:489559687176945664,id_str:489559687176945664,text:at christmas i no more desire a rose than wish a snow in may’s new-fangled mirth,source:\u003ca href=\http:\/\/twitter.com\/download\/iphone\ rel=\nofollow\\u003etwitter for iphone\u003c\/a\u003e,truncated:false,in_reply_to_status_id:null,in_reply_to_status_id_str:null,in_reply_to_user_id:null,in_reply_to_user_id_str:null,in_reply_to_screen_name:null,user:{id:363819213,id_str:363819213,name:ivanna010394,screen_name:ivannacarrillo,location:,url:null,description:null,protected:false,verified:false,followers_count:243,friends_count:530,listed_count:0,favourites_count:26,statuses_count:5672,created_at:sun aug 28 18:58:49 +0000 2011,utc_offset:-14400,time_zone:eastern time (us & canada),geo_enabled:false,lang:es,contributors_enabled:false,is_translator:false,profile_background_color:642d8b,profile_background_image_url:http:\/\/pbs.twimg.com\/profile_background_images\/767201253\/661eb2d4915e9ee6566647dcbaab0186.jpeg,profile_background_image_url_https:https:\/\/pbs.twimg.com\/profile_background_images\/767201253\/661eb2d4915e9ee6566647dcbaab0186.jpeg,profile_background_tile:true,profile_link_color:ff0000,profile_sidebar_border_color:ffffff,profile_sidebar_fill_color:7ac3ee,profile_text_color:3d1957,profile_use_background_image:true,profile_image_url:http:\/\/pbs.twimg.com\/profile_images\/455873054703648768\/_b4mf6o7_normal.jpeg,profile_image_url_https:https:\/\/pbs.twimg.com\/profile_images\/455873054703648768\/_b4mf6o7_normal.jpeg,profile_banner_url:https:\/\/pbs.twimg.com\/profile_banners\/363819213\/1402261141,default_profile:false,default_profile_image:false,following:null,follow_request_sent:null,notifications:null},geo:null,coordinates:null,place:null,contributors:null,retweeted_status:{created_at:wed jul 16 13:45:28 +0000 2014,id:489405458168709120,id_str:489405458168709120,text:our milan show is now sold out, thankyou :d tickets are still available for most of europe ! http:\/\/t.co\/arnh7pvoap http:\/\/t.co\/t5wzyocrtu,source:\u003ca href=\http:\/\/twitter.com\ rel=\nofollow\\u003etwitter web client\u003c\/a\u003e,truncated:false,in_reply_to_status_id:null,in_reply_to_status_id_str:null,in_reply_to_user_id:null,in_reply_to_user_id_str:null,in_reply_to_screen_name:null,user:{id:264107729,id_str:264107729,name:5 seconds of summer,screen_name:5sos,location:sydney, australia,url:http:\/\/www.facebook.com\/5secondsofsummer,description:4 aussies making music :) love the people who support us! our album is out :) http:\/\/po.st\/or93y4 | #ashton5sos #calum5sos #michael5sos #luke5sos,protected:false,verified:true,followers_count:3704204,friends_count:28660,listed_count:20024,favourites_count:1061,statuses_count:17297,created_at:fri mar 11 10:18:46 +0000 2011,utc_offset:36000,time_zone:sydney,geo_enabled:false,lang:en,contributors_enabled:false,is_translator:false,profile_background_color:000000,profile_background_image_url:http:\/\/pbs.twimg.com\/profile_background_images\/483531430371147778\/0gzkh2zi.jpeg,profile_background_image_url_https:https:\/\/pbs.twimg.com\/profile_background_images\/483531430371147778\/0gzkh2zi.jpeg,profile_background_tile:false,profile_link_color:c21b1b,profile_sidebar_border_color:ffffff,profile_sidebar_fill_color:ddeef6,profile_text_color:333333,profile_use_background_image:true,profile_image_url:http:\/\/pbs.twimg.com\/profile_images\/485730748574752768\/zm1ctcvv_normal.jpeg,profile_image_url_https:https:\/\/pbs.twimg.com\/profile_images\/485730748574752768\/zm1ctcvv_normal.jpeg,profile_banner_url:https:\/\/pbs.twimg.com\/profile_banners\/264107729\/1404117825,default_profile:false,default_profile_image:false,following:null,follow_request_sent:null,notifications:null},geo:null,coordinates:null,place:null,contributors:null,retweet_count:12648,favorite_count:31390,entities:{hashtags:[],trends:[],urls:[{url:http:\/\/t.co\/arnh7pvoap,expanded_url:http:\/\/5sos.com\/live,display_url:5sos.com\/live,indices:[93,115]}],user_mentions:[],symbols:[],media:[{id:489405457111715840,id_str:489405457111715840,indices:[116,138],media_url:http:\/\/pbs.twimg.com\/media\/bsq3q5zieaakbgg.jpg,media_url_https:https:\/\/pbs.twimg.com\/media\/bsq3q5zieaakbgg.jpg,url:http:\/\/t.co\/t5wzyocrtu,display_url:pic.twitter.com\/t5wzyocrtu,expanded_url:http:\/\/twitter.com\/5sos\/status\/489405458168709120\/photo\/1,type:photo,sizes:{small:{w:340,h:613,resize:fit},thumb:{w:150,h:150,resize:crop},medium:{w:600,h:1081,resize:fit},large:{w:811,h:1461,resize:fit}}}]},favorited:false,retweeted:false,possibly_sensitive:false,filter_level:low,lang:en},retweet_count:0,favorite_count:0,entities:{hashtags:[],trends:[],urls:[{url:http:\/\/t.co\/arnh7pvoap,expanded_url:http:\/\/5sos.com\/live,display_url:5sos.com\/live,indices:[103,125]}],user_mentions:[{screen_name:5sos,name:5 seconds of summer,id:264107729,id_str:264107729,indices:[3,8]}],symbols:[],media:[{id:489405457111715840,id_str:489405457111715840,indices:[126,140],media_url:http:\/\/pbs.twimg.com\/media\/bsq3q5zieaakbgg.jpg,media_url_https:https:\/\/pbs.twimg.com\/media\/bsq3q5zieaakbgg.jpg,url:http:\/\/t.co\/t5wzyocrtu,display_url:pic.twitter.com\/t5wzyocrtu,expanded_url:http:\/\/twitter.com\/5sos\/status\/489405458168709120\/photo\/1,type:photo,sizes:{small:{w:340,h:613,resize:fit},thumb:{w:150,h:150,resize:crop},medium:{w:600,h:1081,resize:fit},large:{w:811,h:1461,resize:fit}},source_status_id:489405458168709120,source_status_id_str:489405458168709120}]},favorited:false,retweeted:false,possibly_sensitive:false,filter_level:medium,lang:en}
{created_at:sat jan 16 12:00:48 +0000 2016,id:688330056410755072,id_str:688330056410755072,text:i was going to bake a cake and listen to the football. flour refund?,source:\u003ca href=\http:\/\/twitter.com\/download\/iphone\ rel=\nofollow\\u003etwitter for iphone\u003c\/a\u003e,truncated:false,in_reply_to_status_id:null,in_reply_to_status_id_str:null,in_reply_to_user_id:null,in_reply_to_user_id_str:null,in_reply_to_screen_name:null,user:{id:252303653,id_str:252303653,name:pete blackman,screen_name:peteblackman,location:null,url:null,description:null,protected:false,verified:false,followers_count:409,friends_count:903,listed_count:18,favourites_count:5664,statuses_count:22919,created_at:mon feb 14 22:44:37 +0000 2011,utc_offset:3600,time_zone:amsterdam,geo_enabled:false,lang:en,contributors_enabled:false,is_translator:false,profile_background_color:c0deed,profile_background_image_url:http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png,profile_background_image_url_https:https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png,profile_background_tile:false,profile_link_color:0084b4,profile_sidebar_border_color:c0deed,profile_sidebar_fill_color:ddeef6,profile_text_color:333333,profile_use_background_image:true,profile_image_url:http:\/\/pbs.twimg.com\/profile_images\/2600097910\/image_normal.jpg,profile_image_url_https:https:\/\/pbs.twimg.com\/profile_images\/2600097910\/image_normal.jpg,default_profile:true,default_profile_image:false,following:null,follow_request_sent:null,notifications:null},geo:null,coordinates:null,place:null,contributors:null,is_quote_status:false,retweet_count:0,favorite_count:0,entities:{hashtags:[],urls:[],user_mentions:[],symbols:[]},favorited:false,retweeted:false,filter_level:low,lang:en,timestamp_ms:1452945648659}
Or is there any other way but using split? I would really appreciate your tips.
The error is as below.
16/09/18 22:49:37 ERROR TaskSetManager: Task 0 in stage 0.0 failed 1 times; aborting job
Hi hope I understand the question correctly, you are attempting to read a file and with the text mentioned above and then print the "text" mentioned in file containing json
If the above assumption is correct, here a simple code which would do this:
val matchingPattern = "(?i)(text:)(.+?)(,source:)".r
val tweets = scala.io.Source.fromPath("/home/tobby/data/shortTwitter.txt").getLines.reduceLeft(_+_)
matchingPattern.findAllIn(tweets).matchData foreach { m => println(m.group(2)) }
Hope it helps, if the above assumption is not correct please provide a sample input and expected output

txt (from searchTwitter REST API) into JSON with R

I've been using the searchTwitter function from the Twitter REST API to retrieve a certain amount of tweets and I've dumped this to a TXT file.
The structure of this TXT file is:
"text" "favorited" "favoriteCount" "replyToSN" "created" "truncated" "replyToSID" "id" "replyToUID" "statusSource" "screenName" "retweetCount" "isRetweet" "retweeted" "longitude" "latitude"
"1" "RT #kobebryant: Last night was the final chapter to an incredible story. I walk away at peace knowing my love for the game & this city will…" FALSE 0 NA 2016-04-14 23:59:59 FALSE NA "720763566027096066" NA "Twitter for iPhone" "JtLONGWAY" 204125 TRUE FALSE NA NA
"2" "RT #kobebryant: Last night was the final chapter to an incredible story. I walk away at peace knowing my love for the game & this city will…" FALSE 0 NA 2016-04-14 23:59:59 FALSE NA "720763566014332928" NA "Twitter for Android" "Mr_Wizrd" 204125 TRUE FALSE NA NA
"3" "RT #MagicJohnson: I got a chance to get to know #kobebryant away from the court at the #Dodgers game! #ThankYouKobe #KB20 https://twitter.com/sVsW…" FALSE 0 NA 2016-04-14 23:59:59 FALSE NA "720763563783110661" NA "Twitter for iPhone" "TynashKobe" 777 TRUE FALSE NA NA
and I would like to have this as a JSON structure, i.e.
{"created_at":"Wed Apr 13 22:06:02 +0000 2016","id":720372500065071104,"id_str":"720372500065071104","text":"RT #STAPLESCenter: This is where #kobebryant will hold is final press conference tonight. #ThankYouKobe https:\/\/t.co\/1rTiq5eAS9","source":"\u003ca href=\"http:\/\/tweetlogix.com\" rel=\"nofollow\"\u003eTweetlogix\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":149681225,"id_str":"149681225","name":"SP","screen_name":"Mr_LayedBak","location":"West side of Detroit","url":null,"description":"Unfollow me if you're easily offended","protected":false,"verified":false,"followers_count":4326,"friends_count":597,"listed_count":105,"favourites_count":371,"statuses_count":227845,"created_at":"Sat May 29 23:21:29 +0000 2010","utc_offset":-14400,"time_zone":"Eastern Time (US & Canada)","geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/736248613\/7d89d45f16e6c4e508a883aded1aac64.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/736248613\/7d89d45f16e6c4e508a883aded1aac64.jpeg","profile_background_tile":true,"profile_link_color":"141313","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"660A0A","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/719706881736974341\/XT8R51s8_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/719706881736974341\/XT8R51s8_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/149681225\/1452265608","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Wed Apr 13 21:37:36 +0000 2016","id":720365343500144640,"id_str":"720365343500144640","text":"This is where #kobebryant will hold is final press conference tonight. #ThankYouKobe https:\/\/t.co\/1rTiq5eAS9","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":28725783,"id_str":"28725783","name":"STAPLES Center","screen_name":"STAPLESCenter","location":"Los Angeles","url":"http:\/\/www.staplescenter.com","description":"Sports and Entertainment Center of the World located in downtown Los Angeles #LALIVE since 1999. Instagram: #staplescenterla","protected":false,"verified":true,"followers_count":82891,"friends_count":10907,"listed_count":862,"favourites_count":1905,"statuses_count":11024,"created_at":"Sat Apr 04 03:04:17 +0000 2009","utc_offset":-25200,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/553367185700036609\/q6Kh8Ru8.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/553367185700036609\/q6Kh8Ru8.jpeg","profile_background_tile":true,"profile_link_color":"009999","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/2394735481\/7rom2fzqu1vwrq94yzll_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/2394735481\/7rom2fzqu1vwrq94yzll_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/28725783\/1416251684","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":264,"favorite_count":439,"entities":{"hashtags":[{"text":"ThankYouKobe","indices":[71,84]}],"urls":[],"user_mentions":[{"screen_name":"kobebryant","name":"Kobe Bryant","id":1059194370,"id_str":"1059194370","indices":[14,25]}],"symbols":[],"media":[{"id":720365333593260032,"id_str":"720365333593260032","indices":[85,108],"media_url":"http:\/\/pbs.twimg.com\/media\/Cf9AdElVAAA7BqM.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/Cf9AdElVAAA7BqM.jpg","url":"https:\/\/t.co\/1rTiq5eAS9","display_url":"pic.twitter.com\/1rTiq5eAS9","expanded_url":"http:\/\/twitter.com\/STAPLESCenter\/status\/720365343500144640\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":425,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1024,"h":1280,"resize":"fit"},"medium":{"w":600,"h":750,"resize":"fit"}}}]},"extended_entities":{"media":[{"id":720365333593260032,"id_str":"720365333593260032","indices":[85,108],"media_url":"http:\/\/pbs.twimg.com\/media\/Cf9AdElVAAA7BqM.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/Cf9AdElVAAA7BqM.jpg","url":"https:\/\/t.co\/1rTiq5eAS9","display_url":"pic.twitter.com\/1rTiq5eAS9","expanded_url":"http:\/\/twitter.com\/STAPLESCenter\/status\/720365343500144640\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":425,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1024,"h":1280,"resize":"fit"},"medium":{"w":600,"h":750,"resize":"fit"}}}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"is_quote_status":false,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"ThankYouKobe","indices":[90,103]}],"urls":[],"user_mentions":[{"screen_name":"STAPLESCenter","name":"STAPLES Center","id":28725783,"id_str":"28725783","indices":[3,17]},{"screen_name":"kobebryant","name":"Kobe Bryant","id":1059194370,"id_str":"1059194370","indices":[33,44]}],"symbols":[],"media":[{"id":720365333593260032,"id_str":"720365333593260032","indices":[104,127],"media_url":"http:\/\/pbs.twimg.com\/media\/Cf9AdElVAAA7BqM.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/Cf9AdElVAAA7BqM.jpg","url":"https:\/\/t.co\/1rTiq5eAS9","display_url":"pic.twitter.com\/1rTiq5eAS9","expanded_url":"http:\/\/twitter.com\/STAPLESCenter\/status\/720365343500144640\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":425,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1024,"h":1280,"resize":"fit"},"medium":{"w":600,"h":750,"resize":"fit"}},"source_status_id":720365343500144640,"source_status_id_str":"720365343500144640","source_user_id":28725783,"source_user_id_str":"28725783"}]},"extended_entities":{"media":[{"id":720365333593260032,"id_str":"720365333593260032","indices":[104,127],"media_url":"http:\/\/pbs.twimg.com\/media\/Cf9AdElVAAA7BqM.jpg","media_url_https":"https:\/\/pbs.twimg.com\/media\/Cf9AdElVAAA7BqM.jpg","url":"https:\/\/twitter.com\/1rTiq5eAS9","display_url":"pic.twitter.com\/1rTiq5eAS9","expanded_url":"http:\/\/twitter.com\/STAPLESCenter\/status\/720365343500144640\/photo\/1","type":"photo","sizes":{"small":{"w":340,"h":425,"resize":"fit"},"thumb":{"w":150,"h":150,"resize":"crop"},"large":{"w":1024,"h":1280,"resize":"fit"},"medium":{"w":600,"h":750,"resize":"fit"}},"source_status_id":720365343500144640,"source_status_id_str":"720365343500144640","source_user_id":28725783,"source_user_id_str":"28725783"}]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1460585162546"}
I've been trying to load the TXT file with the read.csv(file, header = TRUE, sep ="") and the 1st problem I've found is that since the TXT is formed having the white space as separator for the header, then I get an error saying that there are more columns in the rows than in the header (of course as I'm trying to process also the text from the tweets).
If I don't specify the separator (i.e. read.csv(file)) and I dump the content in a dataframe, then I only get 1 column.
Any hint?
You could do something like
txt <- readLines("myfile.txt")
df <- read.table(text=sub("\\d+-\\d+-\\d+ \\d+:\\d+:\\d+", '"\\1"', txt), header=T)
library(jsonlite)
toJSON(df)
# [{"text":"RT #kobebryant: Last night was the final chapter to an incredible story. I walk ...
Problems arise, because the datetime column created is not wrapped in quotes. Thus, date and time get separated - and the number of header fields does not match anymore. (This simple approach may break if there are for example similar patterns in the textcolumn.)

JSONDecodeError when iterating twitter data

I'm trying to iterate twitter data which is stored in a json file:
fname = 'test.json'
with open(fname, 'r') as f:
for line in f:
tweet = json.loads(line)['text']
print(tweet)
It prints the first tweet in the file just fine but when it iterates for a second time it gives me a JSONDecodeError:
JSONDecodeError: Expecting value: line 2 column 1 (char 1)
My JSON file is 650Mb is size approximately.
To get the twitter data I used the StreamListener from the Twitter API.
Here is a glimpse into my JSON file:
{"created_at":"Sun Apr 24 05:37:02 +0000 2016","id":724109877732204544,"id_str":"724109877732204544","text":"JONES RETURNS WITH A UNANIMOUS DECISION WIN IVER OVINCE SAINT PREUX! #UFC197 https:\/\/t.co\/KlfaAh9h21","source":"\u003ca href=\"http:\/\/instagram.com\" rel=\"nofollow\"\u003eInstagram\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":714389668633116672,"id_str":"714389668633116672","name":"Leon Doyle","screen_name":"TheLDPodcast","location":"Dublin, Ireland","url":"http:\/\/www.youtube.com","description":"A weekly\/bi-weekly podcast focused mainly around MMA, Boxing, fighting etc. With the occasional random topic.","protected":false,"verified":false,"followers_count":7,"friends_count":59,"listed_count":0,"favourites_count":3,"statuses_count":31,"created_at":"Mon Mar 28 09:52:24 +0000 2016","utc_offset":null,"time_zone":null,"geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"000000","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme1\/bg.png","profile_background_tile":false,"profile_link_color":"004455","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"000000","profile_text_color":"000000","profile_use_background_image":false,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/714390864030797824\/REXXKCvs_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/714390864030797824\/REXXKCvs_normal.jpg","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"UFC197","indices":[69,76]}],"urls":[{"url":"https:\/\/t.co\/KlfaAh9h21","expanded_url":"https:\/\/www.instagram.com\/p\/BEkk6Gewpqy\/","display_url":"instagram.com\/p\/BEkk6Gewpqy\/","indices":[77,100]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1461476222819"}
{"created_at":"Sun Apr 24 05:37:03 +0000 2016","id":724109879200366592,"id_str":"724109879200366592","text":"regrann from #ufc - #AndStill UFC flyweight champ #MightyMouseUFC! #UFC197\n\nPresented by\u2026 https:\/\/t.co\/zbE5CsFxMJ","source":"\u003ca href=\"http:\/\/instagram.com\" rel=\"nofollow\"\u003eInstagram\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1070221260,"id_str":"1070221260","name":"Will Manuel","screen_name":"TheWillManuel","location":"Kenai, AK","url":null,"description":"Alaskan. Paramedic. Firefighter. Industrial Security. Libertarian. 2nd Amendment. Liberty. BJJ & Muay Thai novice. #TeamRed #RedemptionMMA #BJJ #MuayThai #MMA","protected":false,"verified":false,"followers_count":437,"friends_count":573,"listed_count":32,"favourites_count":2516,"statuses_count":3184,"created_at":"Tue Jan 08 07:22:47 +0000 2013","utc_offset":-28800,"time_zone":"Alaska","geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"C0DEED","profile_background_image_url":"http:\/\/pbs.twimg.com\/profile_background_images\/579042288040435713\/VeA-zI45.jpeg","profile_background_image_url_https":"https:\/\/pbs.twimg.com\/profile_background_images\/579042288040435713\/VeA-zI45.jpeg","profile_background_tile":true,"profile_link_color":"4A913C","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/715188796615237632\/JvxeLz8D_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/715188796615237632\/JvxeLz8D_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1070221260\/1447179132","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"AndStill","indices":[22,31]},{"text":"UFC197","indices":[69,76]}],"urls":[{"url":"https:\/\/t.co\/zbE5CsFxMJ","expanded_url":"https:\/\/www.instagram.com\/p\/BEkk6a0QMeX\/","display_url":"instagram.com\/p\/BEkk6a0QMeX\/","indices":[92,115]}],"user_mentions":[{"screen_name":"ufc","name":"#UFC197","id":6446742,"id_str":"6446742","indices":[13,17]},{"screen_name":"MightyMouseUFC","name":"Demetrious Johnson","id":140845817,"id_str":"140845817","indices":[52,67]}],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1461476223169"}
{"created_at":"Sun Apr 24 05:37:03 +0000 2016","id":724109882341896192,"id_str":"724109882341896192","text":"RT #BESTFlGHTS: Jon Jones flips off Daniel Cormier at #UFC197 https:\/\/t.co\/S0pDvRWhfW","source":"\u003ca href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\"\u003eTwitter for iPhone\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1019191860,"id_str":"1019191860","name":"Paul","screen_name":"Paulie_Frat","location":"Mount Pocono, PA","url":null,"description":"...","protected":false,"verified":false,"followers_count":272,"friends_count":259,"listed_count":0,"favourites_count":1580,"statuses_count":1622,"created_at":"Tue Dec 18 07:10:12 +0000 2012","utc_offset":-14400,"time_zone":"Eastern Time (US & Canada)","geo_enabled":true,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":true,"profile_link_color":"009999","profile_sidebar_border_color":"EEEEEE","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/512140999444164608\/4H2fiOtg_normal.jpeg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/512140999444164608\/4H2fiOtg_normal.jpeg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1019191860\/1461422809","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":{"created_at":"Sun Apr 24 05:12:13 +0000 2016","id":724103630702432256,"id_str":"724103630702432256","text":"Jon Jones flips off Daniel Cormier at #UFC197 https:\/\/t.co\/S0pDvRWhfW","source":"\u003ca href=\"http:\/\/bufferapp.com\" rel=\"nofollow\"\u003eBuffer\u003c\/a\u003e","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":{"id":1370712786,"id_str":"1370712786","name":"BEST FIGHTS","screen_name":"BESTFlGHTS","location":"MMA, Boxing, Street Fights","url":"http:\/\/snapchat.com\/add\/wshhfans","description":"Parody, we do not own the content posted DM's are open send me your fight","protected":false,"verified":false,"followers_count":156257,"friends_count":17861,"listed_count":83,"favourites_count":1,"statuses_count":6723,"created_at":"Sun Apr 21 22:43:19 +0000 2013","utc_offset":-25200,"time_zone":"Arizona","geo_enabled":false,"lang":"en","contributors_enabled":false,"is_translator":false,"profile_background_color":"131516","profile_background_image_url":"http:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_image_url_https":"https:\/\/abs.twimg.com\/images\/themes\/theme14\/bg.gif","profile_background_tile":true,"profile_link_color":"ABB8C2","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"EFEFEF","profile_text_color":"333333","profile_use_background_image":true,"profile_image_url":"http:\/\/pbs.twimg.com\/profile_images\/620356388833734657\/NvmkmGDk_normal.jpg","profile_image_url_https":"https:\/\/pbs.twimg.com\/profile_images\/620356388833734657\/NvmkmGDk_normal.jpg","profile_banner_url":"https:\/\/pbs.twimg.com\/profile_banners\/1370712786\/1460756748","default_profile":false,"default_profile_image":false,"following":null,"follow_request_sent":null,"notifications":null},"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":740,"favorite_count":624,"entities":{"hashtags":[{"text":"UFC197","indices":[38,45]}],"urls":[{"url":"https:\/\/t.co\/S0pDvRWhfW","expanded_url":"http:\/\/vine.co\/v\/iU5T53X6U7J","display_url":"vine.co\/v\/iU5T53X6U7J","indices":[46,69]}],"user_mentions":[],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en"},"is_quote_status":false,"retweet_count":0,"favorite_count":0,"entities":{"hashtags":[{"text":"UFC197","indices":[54,61]}],"urls":[{"url":"https:\/\/t.co\/S0pDvRWhfW","expanded_url":"http:\/\/vine.co\/v\/iU5T53X6U7J","display_url":"vine.co\/v\/iU5T53X6U7J","indices":[62,85]}],"user_mentions":[{"screen_name":"BESTFlGHTS","name":"BEST FIGHTS","id":1370712786,"id_str":"1370712786","indices":[3,14]}],"symbols":[]},"favorited":false,"retweeted":false,"possibly_sensitive":false,"filter_level":"low","lang":"en","timestamp_ms":"1461476223918"}
How can I solve this issue?
If your JSON file has exactly the same structure as the piece you are posting, the empty lines between tweets indeed cause a JSONDecodeError. If that's the problem, just check that the line is not empty before processing:
In [12]:
with open(fname, 'r') as f:
for line in f:
if (not line.strip()):
continue
tweet = json.loads(line)['text']
print(tweet)
Hope it helps.