Giter Site home page Giter Site logo

Comments (12)

yamafaktory avatar yamafaktory commented on May 22, 2024 1

@shouze I'll check that tomorrow 🙂.

from jql.

shouze avatar shouze commented on May 22, 2024 1

@shouze just to clarify, you want a way to select the nth key of an object (eventually multiple keys by their index), correct?

Yes you're right! 💯

from jql.

shouze avatar shouze commented on May 22, 2024 1

@yamafaktory you're on 🔥 😄

Very nice and clear! Will replace my jq use case by jql with that new feature 💯

from jql.

shouze avatar shouze commented on May 22, 2024 1

@yamafaktory thanks a lot 🙇 , I comfirm that it works like a charm if I pipe this kind of filter on my (more complex thant my initial issue example) json payload: |[0]{[0]}|"0".

from jql.

shouze avatar shouze commented on May 22, 2024

I can run

jql -r '..[]|"key1"'

On this payload:

[
  {
    "key1": [
      {
        "subkey1": "value"
      }
    ]
  },
  {
    "key1": [
      {
        "subkey2": "value"
      }
    ]
  }
]

that produces the expected output, but this is not the same input, and in my case very useless as I haven't a deterministic value for the equivalent of this "key1" key.

from jql.

yamafaktory avatar yamafaktory commented on May 22, 2024

Hi @shouze , jql is not intended to be a full replacement of jq. The grammar / query syntax is definitely different.

from jql.

shouze avatar shouze commented on May 22, 2024

@yamafaktory yes of course, I was just wondering if I could achieve this payload processing with jql.

from jql.

yamafaktory avatar yamafaktory commented on May 22, 2024

@shouze I've checked and indeed this is definitely not something jql was designed for. I fully understand the intent there - though the original jq command you have posted is honestly something I'd not try to implement as it looks so unintuitive too me 👾.

Basically, we we'll need some kind of new feature / grammar definition to be able to select the first key of an object.

I'll think about wether or not this is something jql should provide/ benefit from or not. Furthermore, if true, I'll need to see how this new grammar rule should look like without breaking anything (and without all the pitfalls from jq https://github.com/stedolan/jq/wiki/How-to:-Avoid-Pitfalls).

from jql.

shouze avatar shouze commented on May 22, 2024

@yamafaktory thanks a lot for checking 🙏.

I was not sure but yes I've read the pest grammar file yesterday and it probably miss a reserved keyword or symbol to design whatever key. Could be * or % for example? Thus would allow to write ..[]|% instead of ..[]|"key1" and would solve the dynamic sibling.
I don't know how complicated it could be to implement this feature after extending the grammar, neither how intuitive and aligned with your jql vision it is.

from jql.

yamafaktory avatar yamafaktory commented on May 22, 2024

@shouze just to clarify, you want a way to select the nth key of an object (eventually multiple keys by their index), correct? Your example with a * is not going to work if there are more than one:

[
  {
    "key1": [
      {
        "subkey1": "value"
      }
    ]
  },
  {
    "key2": [
      {
        "subkey2": "value"
      }
    ],
    "key3": "woot"
  }
]

from jql.

yamafaktory avatar yamafaktory commented on May 22, 2024

@shouze hey there 🖖.

Just a little update. I'm working on a branch that will soon introduce a new feature. The core idea is to introduce a new syntax for property selection which is going to help with your issue.

Currently property selection is limited to picking some keys of a given object - in any arbitrary order - see https://github.com/yamafaktory/jql#property-selection.

I'll now make it possible to select properties by their indexes and even using a range. This of course introduce some more complexity since chaining filters e.g. must keep the ongoing transformation of the JSON data working.

Let's have an example which is indirectly covering your initial issue. This is a.json:

{
  "a": [
    {
      "key1": [
        {
          "subkey1": "value"
        }
      ],
      "key2": "a"
    },
    {
      "key2": [
        {
          "subkey2": "value"
        }
      ],
      "key3": "b",
      "key4": "c"
    }
  ]
}

Now let's first go step by step in order to highlight how it's working (please don't mind the cargo run here, that will be just jql for you when released):

cargo run '."a"|{[0]}' a.json
[
  {
    "0": [
      {
        "subkey1": "value"
      }
    ]
  },
  {
    "0": [
      {
        "subkey2": "value"
      }
    ]
  }
]

As you can see the idea is to remap the properties' names to be indexes. This way it works for multiple ones and for ranges too. Next obvious step, filter by the index:

cargo run '."a"|{[0]}|"0"' a.json
[
  [
    {
      "subkey1": "value"
    }
  ],
  [
    {
      "subkey2": "value"
    }
  ]
]

Almost there! We just need another filter or the spread syntax to get rid of the nested arrays. Final one-liner:

cargo run '."a"|{[0]}|"0"|[0]' a.json
cargo run '.."a"|{[0]}|"0"' a.json
[
  {
    "subkey1": "value"
  },
  {
    "subkey2": "value"
  }
]

I already have a bunch of new tests but I'll need some other ones for some corner cases. I'll also need to update the documentation. I'll keep you updated 🛠️.

from jql.

yamafaktory avatar yamafaktory commented on May 22, 2024

@shouze I've just released a new version https://crates.io/crates/jql/3.0.0 (your own v3 - a.k.a. shouze version). I should now work as expected - with the jql syntax of course!

Feel free to open a new issue if something doesn't work as expected 🙇‍♂️.

from jql.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.