0

I have below mentioned dataframe:

ID         Json_Data
PA-124     Json String
PA-145     Json String
PA-185     Json String
PA-149     Json String

I want to extract all the json field corresponding to each ID as mentioned below.

And I would like to know how to extract some specific field against each particular ID (i.e Country).

ID        Name       Country      Code
PA-124    Ajre       Dubai        154
PA-145    Aret       USA          485
PA-185    Cret       Russia       445
PA-149    Oerp       Canada       789

For that i have used below mentioned code, but it returns value against only one ID out of ~1500 IDs.

My Code:

    library(RJSONIO)
    library(rjson)
    library(jsonlite)
    library(data.table)


    f <- function(json, id){
      tmp    <- jsonlite::fromJSON(json)
      tmp    <- as.data.frame(tmp)
      tmp$id <- id
      return(tmp)
    }

json_dfs <- 
  mapply(f, Df$Json_Data, Df$ID, SIMPLIFY = FALSE)

Dput:

    structure(list(ID = c("PA-124", "PA-124", "PA-124"), Json_Data = c("{\n  \"hed\": {\n    \"srti\": \"RT1455\"\n  },\n  \"nnaesr\": {\n    \"name\": \"\",\n    \"firt\": \"alex\",\n    \"last\": \"john\",\n    \"birth\": \"26061981\",\n    \"gender\": \"Male\"\n  },\n  \"iteret\": {\n    \"adre\": \"asded14eer\",\n    \"adfe\": \"\",\n    \"sfdet\": \"\",\n    \"sfeet\": \"\",\n    \"serted\": \"\",\n    \"sertgf\": \"\"\n  },\n  \"sefetgd\": {\n    \"mobe\": \"2111111\",\n    \"dfete\": \"\",\n    \"tfet\": \"sdfet\"\n  },\n  \"aarfgeette\": {\n    \"aarfgertte\": \"H NO. , dffr rtrg\",\n    \"aarfgot2\": \"ids School, feeeete,\",\n    \"aarfgot3\": \"\",\n    \"aarfgot4\": \"\",\n    \"aarfgot5\": \"\",\n    \"sdfffy\": \"errtyy\",\n    \"errrte\": \"efeteet\",\n    \"sdetVb\": \"\",\n    \"sdfes\": \"5500\",\n    \"aarfgppr\": \"eretft er\",\n    \"seferf\": \"ered\"\n  }\n}", 
"{\n  \"hed\": {\n    \"srti\": \"RT1455\"\n  },\n  \"nnaesr\": {\n    \"name\": \"\",\n    \"firt\": \"alex\",\n    \"last\": \"john\",\n    \"birth\": \"26061981\",\n    \"gender\": \"Male\"\n  },\n  \"iteret\": {\n    \"adre\": \"asded14eer\",\n    \"adfe\": \"\",\n    \"sfdet\": \"\",\n    \"sfeet\": \"\",\n    \"serted\": \"\",\n    \"sertgf\": \"\"\n  },\n  \"sefetgd\": {\n    \"mobe\": \"2111111\",\n    \"dfete\": \"\",\n    \"tfet\": \"sdfet\"\n  },\n  \"aarfgeette\": {\n    \"aarfgertte\": \"efeet efet\",\n    \"aarfgot2\": \"affe fe,\",\n    \"aarfgot3\": \"\",\n    \"aarfgot4\": \"\",\n    \"aarfgot5\": \"\",\n    \"sdfffy\": \"Telangana\",\n    \"errrte\": \"Hyderabad\",\n    \"sdetVb\": \"\",\n    \"sdfes\": \"5500\",\n    \"aarfgppr\": \"acefec ee\",\n    \"seferf\": \"dfefe\"\n  }\n}", 
"{\n  \"hed\": {\n    \"srti\": \"RT1455\"\n  },\n  \"nnaesr\": {\n    \"name\": \"\",\n    \"firt\": \"alex\",\n    \"last\": \"john\",\n    \"birth\": \"26061981\",\n    \"gender\": \"Male\"\n  },\n  \"iteret\": {\n    \"adre\": \"asded14eer\",\n    \"adfe\": \"\",\n    \"sfdet\": \"\",\n    \"sfeet\": \"\",\n    \"serted\": \"\",\n    \"sertgf\": \"\"\n  },\n  \"sefetgd\": {\n    \"mobe\": \"2111111\",\n    \"dfete\": \"\",\n    \"tfet\": \"sdfet\"\n  },\n  \"aarfgeette\": {\n    \"aarfgertte\": \"ad.de 25dfege\",\n    \"aarfgot2\": \"adfed ,\",\n    \"aarfgot3\": \"\",\n    \"aarfgot4\": \"\",\n    \"aarfgot5\": \"\",\n    \"sdfffy\": \"adffe\",\n    \"errrte\": \"Hyderabad\",\n    \"sdetVb\": \"\",\n    \"sdfes\": \"5500\",\n    \"aarfgppr\": \"sadfe ad\",\n    \"seferf\": \"dsfde\"\n  }\n}"
)), row.names = c(NA, 3L), class = "data.frame")
Roy1245
  • 507
  • 4
  • 18
  • 1
    Please make your example reproducible. Can you post the `dput` of the first couple of rows? The `"Json string"` would be useful to have. – Axeman May 08 '18 at 11:25
  • @Axeman Thanks Updated the question with json string. – Roy1245 May 08 '18 at 11:37
  • Actually, we prefer the `dput` of the `Df`, not just the json string column. Can you also add `dput(head(Df,3)` to the question? – Shique May 08 '18 at 11:48
  • @Shique I have update the question with dput. – Roy1245 May 08 '18 at 11:56
  • if `dput(head(Df,3))` only returns that, then it means that the `Df` in question only contains 1 row, resulting in what you get (just one ID). You need to put all ID's and their json data in `Df`. – Shique May 08 '18 at 12:02
  • @Shique Update the json – Roy1245 May 08 '18 at 12:19
  • Running that through the `f` function exactly returns a list of 3, not 1. So it works correctly. What seems to be the problem? More importantly; ***what is your desired output***? – Shique May 08 '18 at 12:26
  • @Shique That was a small sample, i am getting this error now: `Error: lexical error: invalid char in json text. NA (right here) ------^ Called from: parse_string(txt, bigint_as_char)` – Roy1245 May 08 '18 at 12:28

1 Answers1

0

You can use unlist to convert it into a character vector and then row bind the list of character vector into a data.frame. Then column bind the results with your ID

cbind(temp$ID,
    do.call(rbind, lapply(temp$Json_Data, function(x) unlist(jsonlite::fromJSON(x))))
)

result:

              hed.srti nnaesr.name nnaesr.firt nnaesr.last nnaesr.birth nnaesr.gender iteret.adre  iteret.adfe iteret.sfdet iteret.sfeet iteret.serted iteret.sertgf
[1,] "PA-124" "RT1455" ""          "alex"      "john"      "26061981"   "Male"        "asded14eer" ""          ""           ""           ""            ""           
[2,] "PA-124" "RT1455" ""          "alex"      "john"      "26061981"   "Male"        "asded14eer" ""          ""           ""           ""            ""           
[3,] "PA-124" "RT1455" ""          "alex"      "john"      "26061981"   "Male"        "asded14eer" ""          ""           ""           ""            ""           
     sefetgd.mobe sefetgd.dfete sefetgd.tfet aarfgeette.aarfgertte aarfgeette.aarfgot2    aarfgeette.aarfgot3 aarfgeette.aarfgot4 aarfgeette.aarfgot5 aarfgeette.sdfffy
[1,] "2111111"    ""            "sdfet"      "H NO. , dffr rtrg"   "ids School, feeeete," ""                  ""                  ""                  "errtyy"         
[2,] "2111111"    ""            "sdfet"      "efeet efet"          "affe fe,"             ""                  ""                  ""                  "Telangana"      
[3,] "2111111"    ""            "sdfet"      "ad.de 25dfege"       "adfed ,"              ""                  ""                  ""                  "adffe"          
     aarfgeette.errrte aarfgeette.sdetVb aarfgeette.sdfes aarfgeette.aarfgppr aarfgeette.seferf
[1,] "efeteet"         ""                "5500"           "eretft er"         "ered"           
[2,] "Hyderabad"       ""                "5500"           "acefec ee"         "dfefe"          
[3,] "Hyderabad"       ""                "5500"           "sadfe ad"          "dsfde"     
chinsoon12
  • 25,005
  • 4
  • 25
  • 35