Giter Site home page Giter Site logo

Filter by root key value about jsonpath HOT 17 CLOSED

joshbuddy avatar joshbuddy commented on August 16, 2024
Filter by root key value

from jsonpath.

Comments (17)

Skarlso avatar Skarlso commented on August 16, 2024 1

Huh. That is odd. I should think that that should work as well. I'll investigate.

from jsonpath.

NDuggan avatar NDuggan commented on August 16, 2024 1

@Skarlso can confirm that it now worked using $.[?(@.initial == true)] which is perfect.

Thanks again for your quick response and action! 🥇

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024 1

You're welcome. Thanks for testing and reporting the issue.

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

Could you please try @.initial just simply? I'll investigate soon.

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

For the record, jsonpath the gem is rather using this as a guide: http://jsonpath.com/ for which the above does not work.

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

Hah! Try this: $.[?(@ == true)]. Since it's the first node, that should work.

@NDuggan

from jsonpath.

NDuggan avatar NDuggan commented on August 16, 2024

Thanks for the reply @Skarlso. I did in fact try using @ == true before posting but that didn't work.

Also without referencing the key you could end up filtering by the wrong element, e.g.:

{
  "initial": false,
  "another": true
}

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

I see the dilemma. I'll try and fix it. :)

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

@NDuggan bamm! :)

  def test_another
    json = {
      initial: true,
      not: true
    }.to_json
    p JsonPath.on(json, "$.initial[?(@)]")
    json = {
      initial: false,
      not: true
    }.to_json
    p JsonPath.on(json, "$.initial[?(@)]")
  end

Output:

# Running:

[true]
[]
.

from jsonpath.

NDuggan avatar NDuggan commented on August 16, 2024

@Skarlso awesome, thank you very much.

I can also confirm that your examples work and the following also works.

json = {initial: 'foo'}.to_json
JsonPath.on(json, "$.initial[?(@ == 'foo')]")
=> ["foo"]

Is there any reason though, that you cannot do this:

json = {initial: true}.to_json
JsonPath.on(json, "$.initial[?(@ == true)]")
=> []

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

Sorry, I'm swamped, going to look at it today maybe, but tomorrow latest.

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

Ah, because I strip out @... so the expression becomes "== true" which won't work. That's why you can't do this. I'm gonna fix it.

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

So this is quiet odd.

$.[?(@.initial == true)] should actually work fine. :/

el: true; operator: ==; operand: true
ret = el.send(operator.strip, operand)

Returns false. :O

While irb:

2.3.1 :001 > true.send("==", true)
 => true

I don't get it.

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

It's a string. :/ Argh. Of course. It's the same problem as with the false thing.

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

works now as it does with Jayway's parser:

{
    "initial": true,
    "second": true,
}

Path: $.[?(@.initial == true)]

Result:

[
   {
      "initial" : true,
      "second" : true
   }
]

@NDuggan Is that okay?

from jsonpath.

Skarlso avatar Skarlso commented on August 16, 2024

Also, fixed the parser so that it properly defines true and false now. :)

from jsonpath.

NDuggan avatar NDuggan commented on August 16, 2024

@Skarlso sorry I was away for a few days, I'm going to pull the latest version and give it a go! 👍

from jsonpath.

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.