0

I am trying to access a value from json by using jsonlite package, but the value (the value of key strongbuy where the period is 0m) I am trying to access has the same key name (period -1m has the same key name), so it returns NA value. Is there a way to do it with jsonlite or with regex?

i<- "MSFT"
    json_object <- getURL(json_url)
    quote_summary <- fromJSON(json_object) %>% unlist()

quote_summary["quoteSummary.result.recommendationTrend.trend.strongBuy"] %>% unname() %>% as.numeric()

Below is the value of json_object:

"{\"quoteSummary\":{\"result\":[{\"recommendationTrend\":{\"trend\":[{\"period\":\"0m\",\"strongBuy\":14,\"buy\":13,\"hold\":6,\"sell\":0,\"strongSell\":1},{\"period\":\"-1m\",\"strongBuy\":12,\"buy\":13,\"hold\":7,\"sell\":0,\"strongSell\":1},{\"period\":\"-2m\",\"strongBuy\":12,\"buy\":13,\"hold\":8,\"sell\":0,\"strongSell\":1},{\"period\":\"-3m\",\"strongBuy\":12,\"buy\":13,\"hold\":9,\"sell\":0,\"strongSell\":1}],\"maxAge\":86400}}],\"error\":null}}"

Below is the value of quote_summary:

    quoteSummary.result.recommendationTrend.trend.period1 
                                                     "0m" 
    quoteSummary.result.recommendationTrend.trend.period2 
                                                    "-1m" 
    quoteSummary.result.recommendationTrend.trend.period3 
                                                    "-2m" 
    quoteSummary.result.recommendationTrend.trend.period4 
                                                    "-3m" 
 quoteSummary.result.recommendationTrend.trend.strongBuy1 
                                                     "14" 
 quoteSummary.result.recommendationTrend.trend.strongBuy2 
                                                     "12" 
 quoteSummary.result.recommendationTrend.trend.strongBuy3 
                                                     "12" 
 quoteSummary.result.recommendationTrend.trend.strongBuy4 
                                                     "12" 
       quoteSummary.result.recommendationTrend.trend.buy1 
                                                     "13" 
       quoteSummary.result.recommendationTrend.trend.buy2 
                                                     "13" 
       quoteSummary.result.recommendationTrend.trend.buy3 
                                                     "13" 
       quoteSummary.result.recommendationTrend.trend.buy4 
                                                     "13" 
      quoteSummary.result.recommendationTrend.trend.hold1 
                                                      "6" 
      quoteSummary.result.recommendationTrend.trend.hold2 
                                                      "7" 
      quoteSummary.result.recommendationTrend.trend.hold3 
                                                      "8" 
      quoteSummary.result.recommendationTrend.trend.hold4 
                                                      "9" 
      quoteSummary.result.recommendationTrend.trend.sell1 
                                                      "0" 
      quoteSummary.result.recommendationTrend.trend.sell2 
                                                      "0" 
      quoteSummary.result.recommendationTrend.trend.sell3 
                                                      "0" 
      quoteSummary.result.recommendationTrend.trend.sell4 
                                                      "0" 
quoteSummary.result.recommendationTrend.trend.strongSell1 
                                                      "1" 
quoteSummary.result.recommendationTrend.trend.strongSell2 
                                                      "1" 
quoteSummary.result.recommendationTrend.trend.strongSell3 
                                                      "1" 
quoteSummary.result.recommendationTrend.trend.strongSell4 
                                                      "1" 
           quoteSummary.result.recommendationTrend.maxAge 
                                                  "86400" 
user9532692
  • 584
  • 7
  • 28

2 Answers2

0

Not sure why you are using unlist. Without it you can access the corresponding table with

quote_summary$quoteSummary$result$recommendationTrend$trend[[1]]

which looks like

  period strongBuy buy hold sell strongSell
1     0m        14  13    6    0          1
2    -1m        12  13    7    0          1
3    -2m        12  13    8    0          1
4    -3m        12  13    9    0          1

and is of class data.frame. From here you can use tidyverse

library(tidyverse)
df %>%
  filter(period == "-1m") %>%
  select(strongBuy)


  strongBuy
1        12

If you want to unlist the results, then use recursive = F so you dont end up with a messy list of length 700+:

unlist(quote_summary$quoteSummary$result, recursive = F)
Martin Schmelzer
  • 23,283
  • 6
  • 73
  • 98
-1

I figured I could just do this.

    strongbuy <- quote_summary["quoteSummary.result.recommendationTrend.trend.strongBuy1"] 
                                      %>% unname() 
                                      %>% as.numeric()
user9532692
  • 584
  • 7
  • 28