1

I have my JSON file and I want to parse it in a list. I wonder if I can parse only the lines with specific string in it? For example

{"app":15c,"device_carrier":"Verizon Wireless"}
{"app":15b,"device_carrier":"Verizon Wireless"}
{"app":15a,"device_carrier":"Verizon Wireless"}

and I want to parse only the lines that have "app":15a I want to have a list with the last line! (The problem is I have a lot of data and my computer can't parse the whole JSON file, and I need only one app)

4
  • Hi Smasell, can you provide an example of the JSON you want to parse, the desired output, and what you have tried so far? Commented Dec 11, 2016 at 13:17
  • The most robust way would be to parse everything and then filter the list. Filtering before parsing is much more tedious as the formatting of the JSON might potentially vary. Commented Dec 11, 2016 at 13:21
  • @MrMobster Yeah I now it! I've edited the question! Is it possible without parsing the whole file? Commented Dec 11, 2016 at 13:31
  • 15c (etc) can't be bare. they are strings Commented Dec 11, 2016 at 13:32

1 Answer 1

1

There are ways to optimize the following but it might not be necessary in your case:

library(jsonlite)
library(dplyr)

df <- data_frame()
jsonlite::stream_in(file("/tmp/apps.json"), 
                    function(x) { 
                        df <<- bind_rows(df, filter(x, app=="15b"))
                     })

I made a file that looks like:

{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}
{"app":"15c","device_carrier":"Verizon Wireless"}
{"app":"15b","device_carrier":"Verizon Wireless"}
{"app":"15a","device_carrier":"Verizon Wireless"}

Run that code and you get:

df
## # A tibble: 13 × 2
##      app   device_carrier
##    <chr>            <chr>
## 1    15b Verizon Wireless
## 2    15b Verizon Wireless
## 3    15b Verizon Wireless
## 4    15b Verizon Wireless
## 5    15b Verizon Wireless
## 6    15b Verizon Wireless
## 7    15b Verizon Wireless
## 8    15b Verizon Wireless
## 9    15b Verizon Wireless
## 10   15b Verizon Wireless
## 11   15b Verizon Wireless
## 12   15b Verizon Wireless
## 13   15b Verizon Wireless
Sign up to request clarification or add additional context in comments.

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.