Giter VIP home page Giter VIP logo

Comments (10)

 avatar commented on July 24, 2024 15

@radostyle For usage questions, please check out Stack Overflow: http://stackoverflow.com/questions/tagged/jq

Also, remember to read the manual first! It has nice sample programs you can use. http://stedolan.github.io/jq/manual/#containselement

As per your example, a sample working program would be select(.hello | contains("hi"))

from jq.

dgryski avatar dgryski commented on July 24, 2024 2

This is a slippery slope towards full regexp support, which actually I think would be a good idea. Easy to add via RE2 or PCRE.

from jq.

stedolan avatar stedolan commented on July 24, 2024

Yeah, this is something you just can't do in jq at the moment. I'll should be fairly straightforward to add a builtin function that checks for substrings.

Later on, I'm definitely considering shoving in PCRE. I want to be a bit careful about dependencies, so I might end up using PCRE with some dlopen trickery so that the rest of jq still runs if the PCRE library is unavailable at runtime. That might be messy, though.

from jq.

stroan avatar stroan commented on July 24, 2024

I'm working on this. I'm currently implementing a more general subset operator.

# string b is a subset of string a if string b is wholely contained in string a
"foobar" =: "foo" -> true
"foobar" =: "baz" -> false

# arrays are treated like ordered lists, not as sets. the subset operator ensures
# that the first array contains all the elements in the second array in the same
# order consecutively. elements are compared using regular equality.
[1,2,3,4] =: [2,3] -> true
[1,2,3,4] =: [3,2] -> false
[1,{foo: 12, bar: 13},3,4] =: [1,{foo: 12, bar:13}] -> true
[1,{foo: 12, bar: 13},3,4] =: [1,{foo: 12}] -> false

# An object b is a subset of object a if for every key in b there is a key in a
# such that if the values are both objects the object from b is a subset of the
# object from a, or if the values are any other type they are equal.
{foo: 12, bar:13} =: {foo: 12} -> true
{foo:12} =: {bar:13} -> false
{foo: {bar: 12, baz: 13}, blap: [1,2,3]} =: {foo: {bar: 12}, blap: [1,2,3]} -> true
{foo: {bar: 12, baz: 13}, blap: [1,2,3]} =: {foo: {bar: 12}, blap: [1,2]} -> false

Thoughts on this, before I pull request the code?

EDIT: fixed one of the examples.

from jq.

stedolan avatar stedolan commented on July 24, 2024

I'd swap the arguments around and spell the operator "in", this syntax is a little cryptic.

I think arrays-as-sets might be more useful, I don't often find myself doing subsequence searches in an array.

When comparing objects, I'm not sure I follow the logic behind recursing into other objects but not arrays. Although I'm not sure any alternative would seem more natural.

If you're implementing a general "containment" operator, it might be nice to support "string" is-a-subset-of "object" to mean "does the object have this key".

Looks cool!

from jq.

stroan avatar stroan commented on July 24, 2024

Fixed by #36. This can be closed.

from jq.

radostyle avatar radostyle commented on July 24, 2024

How do you use this feature?

$ echo '{ "hello":"hi"} ' | jq 'select(.hello contains( "hi"))'
error: syntax error, unexpected IDENT
select(.hello contains( "hi")) 1 compile error

$ echo '{ "hello":"hi"} ' | jq 'select(.hello contains "hi")'
error: syntax error, unexpected IDENT
select(.hello contains "hi") 1 compile error

from jq.

radostyle avatar radostyle commented on July 24, 2024

Thank you Santiago. I did not realize you had to use the pipe operator '|'
as a test. I did read the manual but in the manual there is no example
with the pipe operator. Perhaps I should add one?

On Tue, Jun 23, 2015 at 1:43 PM, Santiago Lapresta <[email protected]

wrote:

@radostyle https://github.com/radostyle For usage questions, please
check out Stack Overflow: http://stackoverflow.com/questions/tagged/jq

Also, remember to read the manual first! It has nice sample programs you
can use. http://stedolan.github.io/jq/manual/#containselement

As per your example, a sample working program would be select(.hello |
contains("hi"))


Reply to this email directly or view it on GitHub
#33 (comment).

from jq.

 avatar commented on July 24, 2024

@radostyle The manual does have a short section about the pipe operator, although, ironically, I can't link you to it since its name is just the pipe character... still, it should be strongly emphasised in the documentation, given that it is perhaps the most essential part of the language, and it certainly is not right now.

Any documentation changes that make jq easier to use are welcome, and heavily encouraged in fact! The documentation is over at docs/content/3.manual/manual.yml; some basic and probably outdated information on how to run the documentation server locally can be found here. Serve yourself!

from jq.

nicowilliams avatar nicowilliams commented on July 24, 2024

from jq.

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.