0

I am working in R with huge JSON files. These JSON files have lists that are nested into lists that are nested into lists (etc...). So that there are multiple levels of elements.

My question is how can I extract only the key elements stored at one specific level without getting the associated values with all his nested lists ?

The files that I am working on look like more and less like this :

{
    "Key 1 at level 1": "value x",
    "Key 2 at level 1": "value x",
    "Key 3 at level 1": {
        "Key 1 at level 2": {
            "Key 1 at level 3": "value x",
            "Key 2 at level 3": "value x",
            "Key 3 at level 3": "value x"
        },
        "Key 2 at level 2": {
            "Key 4 at level 3": "value x",
            "Key 5 at level 3": "value x",
            "Key 6 at level 3": "value x"
        }
    }
}

So, in this example, what I would like is to retrieve a list that would contain "Key 1 at level 2" and "Key 2 at level 2".

You can find one real example in this link : http://bioinfo.hpc.cam.ac.uk/cellbase/webservices/rest/swagger.json (carefull because i's huge)

Sorry if this question has been asked before. I have been spending a long time looking for an answer but I didn't find anything.

Thanks in advance.

Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57
Isa
  • 53
  • 2
  • 9
  • what are the swagger file's keys in question? – hrbrmstr Sep 29 '17 at 11:35
  • also, you want the keys or the values or both? – hrbrmstr Sep 29 '17 at 11:41
  • Thanks for the question. In the real example I provided (swagger link), the keys I would like to retrieve would be those included in the key "paths" (key 5 at first level). As you can see there are several values included in "paths" (precisely 97 items). And each value starts whit another key which has values associated. The first two keys at this second level are "/{version}/meta", "/{version}/meta/about", but I would like to retrieve all the 97. But I just need the keys at this level, and I don't want all the values associated to these keys. Hope it is clear. – Isa Sep 29 '17 at 15:38

1 Answers1

1

In this situation you want the keys inside each of the top-level values. We can do this by mapping each element to it's names.

This will give us a list containing NULLs and character vectors. We unlist to get rid of the NULLs and turn it into a single character vector.

library('purrr')
library('tidyverse')
library('rjson')

swagger <- fromJSON('
  {
      "Key 1 at level 1": "value x",
      "Key 2 at level 1": "value x",
      "Key 3 at level 1": {
          "Key 1 at level 2": {
              "Key 1 at level 3": "value x",
              "Key 2 at level 3": "value x",
              "Key 3 at level 3": "value x"
          },
          "Key 2 at level 2": {
              "Key 4 at level 3": "value x",
              "Key 5 at level 3": "value x",
              "Key 6 at level 3": "value x"
          }
      }
  }
')
map(swagger, names) %>% unlist

[1] "Key 1 at level 2" "Key 2 at level 2"

Paul
  • 8,734
  • 1
  • 26
  • 36
  • Thank you! It was very useful. After applying your suggestion, I made a little change on order to apply it to other files with different levels : > all_keys <- map(swagger, names) > all_keys $`Key 1 at level 1` NULL $`Key 2 at level 1` NULL $`Key 3 at level 1` [1] "Key 1 at level 2" "Key 2 at level 2" > all_keys$"Key 3 at level 1" [1] "Key 1 at level 2" "Key 2 at level 2" – Isa Sep 30 '17 at 14:37