How to filter json based on position?

Hello everybody,

I’m trying to visualize some data with lilaq. The manual recommends loading data from json. So I converted my existing data tables from csv to json.

I now have data that is structured as follows:

{
    "TN1":{
        "TN":1,
        "L1":"de",
        "measurement":[4,7,9,15],
        "exclude":[0,1,0,0],
        "type":["short","short","long","long"]
    },
    "TN2":{
        "TN":2,
        "L1":"es",
        "measurement":[3,8,7,12],
        "exclude":[0,0,1,0],
        "type":["short","short","long","long"]
    }
}

I would like to be able to generate arrays of the “measurement” data, but I need to filter the data based on its position.

For example I’d like to exclude all data points that have a 1 at the same position in the exclude array.

Or I would like to filter to have just the values that are tagged with “short”.

Unfortunately my scripting skills are not sufficient for this task. Can anybody help?

You can join two data points with array.zip, and then filter them appropriately. For example,

#let TNs = json(bytes(```
// JSON data here
```.text))

#let measurements = TNs.values().map(val => {
  array.zip(val.measurement, val.exclude)  // join measurements and exclusions
    .filter(((m, e)) => e == 0)            // remove values where exclude == 1
    .map(((m, e)) => m)                    // only keep the measurment
})

You can hover over closing parentheses to see what each step is doing

2 Likes