adriank / ObjectPath

The agile query language for semi-structured data
http://objectpath.org
MIT License
380 stars 93 forks source link

Support for query by key using data from a list #85

Open jeking3 opened 5 years ago

jeking3 commented 5 years ago

I have some data where there are "named references" to other objects in other parts of the document stored as a string, for example:

{
  "beverage": {"soda": {
    "lemonlime": {"plants": [
      "Atlanta",
      "Chicago"
    ]},
    "rootbeer": {"plants": [
      "Chicago",
      "Denver"
    ]}
  }},
  "manufacturing": {"plant": {
    "Atlanta": {"capacity": 500000},
    "Denver": {"capacity": 300000},
    "Chicago": {"capacity": 200000}
  }}
}

I was unable to find a way to access the appropriate manufacturing plant using ObjectPath as each record is a named dictionary rather than a list item. So if I wanted to find out the total capacity possible for each of the two sodas using ObjectPath, is there a way to do that?

I was trying to do something like this: sum($.manufacturing.plant.[$.beverage.soda.lemonlime.plants].capacity) but I was not able to set a part of the path to the value that is stored somewhere else. All of the selectors appear to be list based, is that correct?

Some information I found elsewhere indicates that if the name of the plant were a value in each record it would be possible, but with the name of the plant as a key to the record itself that there is no lookup directive that can do this. I'd prefer not to have to rewrite these structures just to be able to use ObjectPath to look up information in this way.

If there were another operator or function that could do this, it would be useful:

sum($.manufacturing.plant.&[$.beverage.soda.lemonlime.plants].capacity -> 700000

for example, given .* would be all names, perhaps .& could be selected names?

If the data were structured in another way, where the references were fully qualified dotted names into the tree:

{
  "beverage": {"soda": {
    "lemonlime": {"plants": [
      "manufacturing.plant.Atlanta",
      "manufacturing.plant.Chicago"
    ]},
    "rootbeer": {"plants": [
      "manufacturing.plant.Chicago",
      "manufacturing.plant.Denver"
    ]}
  }},
  "manufacturing": {"plant": {
    "Atlanta": {"capacity": 500000},
    "Denver": {"capacity": 300000},
    "Chicago": {"capacity": 200000}
  }}
}

A desired lookup on this would be:

sum($.&[$.beverage.soda.lemonlime.plants].capacity -> 700000

Thoughts?