ABridoux / scout

Reading and writing in JSON, Plist, YAML and XML data made simple when the data format is not known at build time. Swift library and command-line tool.
https://www.woodys-findings.com/scout
MIT License
130 stars 6 forks source link

Filter data depending on the value #111

Open ABridoux opened 4 years ago

ABridoux commented 4 years ago

Allow to filter a group of values depending on the value of a certain key or if a certain key regardless of its value.

For example, with the Json

{
  "people" : {
    "Tom" : {
      "age" : 68,
      "hobbies" : [
        "cooking",
        "guitar"
      ],
      "height" : 175
    },
    "Suzanne" : {
      "job" : "actress",
      "movies" : [
        {
          "title" : "Tomorrow is so far",
          "awards" : "Best speech for a silent movie"
        },
        {
          "title" : "Yesterday will never go",
          "awards" : "Best title"
        },
        {
          "title" : "What about today?"
        }
      ]
    },
    "Robert" : {
      "age" : 23,
      "hobbies" : [
        "video games",
        "party",
        "tennis"
      ],
      "running_records" : [
        [
          10,
          12,
          9,
          10
        ],
        [
          9,
          12,
          11
        ]
      ],
      "height" : 181
    }
  }
}

scout filter "people.&.age<70" should output

{
  "people" : {
    "Tom" : {
      "age" : 68,
      "hobbies" : [
        "cooking",
        "guitar"
      ],
      "height" : 175
    },
    "Robert" : {
      "age" : 23,
      "hobbies" : [
        "video games",
        "party",
        "tennis"
      ],
      "running_records" : [
        [
          10,
          12,
          9,
          10
        ],
        [
          9,
          12,
          11
        ]
      ],
      "height" : 181
    }
  }
}
haakonstorm commented 3 years ago

I'm mentioning this here because I get a sense you now (regarding filter, match etc) are walking down a path where a brilliant guy has already walked before you: That guy is Hadley Wickham, and what I really think you should play around with is tidyverse (https://www.tidyverse.org). Don't be alarmed. I've coded since I was a kid, everything from assembly, C to Pascal and SALT, and had never heard about the R programming language until a couple of years ago, when I went back to university to study Plant science. Which sorts under Biology, and in this university, all biology students start out on day 1 by learning to code. In R! I'm mentioning this because with "tidyverse" in R, there is a very clear and very coherent way of thinking about and wrestling with data sets. In particular, see what you can find about Wickham writing about "tidy data" as in https://tidyr.tidyverse.org -- and the cheat sheet here https://github.com/rstudio/cheatsheets/blob/master/data-import.pdf visualizes the core philosophy about what makes data tidy.

After skimming through the tidy data stuff, this: https://dplyr.tidyverse.org will make a lot more sense. The way data scientists work with filter and select, with tidy data, is just mind blowing. btw: In reading the code examples, whenever you see %>% this is the R pipe, so to speak. So when you see:

starwars %>% 
  filter(species == "Droid")

... its basically `cat starwars.json | scout [...]´