0

My question may seem trivial, but I am facing issue while parsing a string into json format.

The string I am having is;

[{"Date":"2014-01-01","Turnover":"6761","Shop_Id":"60"},{"Date":"2014-02-01","Turnover":"7254","Shop_Id":"70"},{"Date":"2014-03-01","Turnover":"7539","Shop_Id":"75"},{"Date":"2014-04-01","Turnover":"8211","Shop_Id":"82"}]

I am not able to feed this string in R, while trying to assign this string in an object "v", I am etting this error;

Error: unexpected '[' in "v <- ["

I guess in R, it's not a standard format for a string to be started with a "[". I tried putting quote on the whole string to make it a character, still it's throwing the same error.

I want to parse this string as a json object, but unable to do the same using fromJSON & toJSON. Parsing this text, I want to make it as dataframe, where I will have all three columns viz;

Date   Turnover  Shop_Id

Please let me know using the mentioned string how can I make a dataframe by parsing the same.

TIA

2
  • 1
    Try encasing the entire JSON string in single quotes( ' '). Then pass this variable to fromJSON function. Commented Apr 17, 2020 at 16:14
  • @Dave2e I tried putting a quote but still it's not taking Commented Apr 17, 2020 at 16:22

1 Answer 1

2

use single quotes ' instead of double quotes " to store it as string and Bingo!!!

    library(jsonlite)

    jsonStr <- '[{"Date":"2014-01-01","Turnover":"6761","Shop_Id":"60"},{"Date":"2014-02-01","Turnover":"7254","Shop_Id":"70"},{"Date":"2014-03-01","Turnover":"7539","Shop_Id":"75"},{"Date":"2014-04-01","Turnover":"8211","Shop_Id":"82"}]'

   fromJSON(jsonStr)

Output

   Date        Turnover Shop_Id
 1 2014-01-01     6761      60
 2 2014-02-01     7254      70
 3 2014-03-01     7539      75
 4 2014-04-01     8211      82
Sign up to request clarification or add additional context in comments.

2 Comments

@Hindol Ganguly, I didn't see that Dave2e has already commented that. You might have missed something. Use the above, it will work for sure.
yes.. I have missed that.. Just tried & it worked.. I have upvoted @Dave2e & your's comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.