GithubHelp home page GithubHelp logo

Comments (6)

ohler55 avatar ohler55 commented on June 2, 2024

That is interesting. Sounds like a bug that will need to be fixed.

from ojg.

hspaay avatar hspaay commented on June 2, 2024

Simple test example that shows this.
Query1 returns 1 item in the result. Query 2 returns no items in the result. The only difference is change from query of 'type' to '@type', both using the bracket notation.
Hope this helps.

func TestQuery2(t *testing.T) {
	store := make(map[string]interface{})

	jsondoc := `{
		"item1": {
			"id": "item1",
			"type": "type1",
			"@type": "attype1"
		},
		"item2": {
			"id": "item2",
			"type": "type2",
			"@type": "attype2"
		}
	}`
	query1 := `$[?(@['type']=="type1")]`           // bracket notation without object literal
	query2 := `$[?(@['@type']=="type1")]`          // with object literal 

	err := json.Unmarshal([]byte(jsondoc), &store)
	assert.NoError(t, err)

	jpExpr, err := jp.ParseString(query1)
	assert.NoError(t, err)
	result := jpExpr.Get(store)         // this returns one result as expected
	assert.NotEmpty(t, result)

	jpExpr, err = jp.ParseString(query2)
	assert.NoError(t, err)
	result = jpExpr.Get(store)
	assert.NotEmpty(t, result)      // this fails with empty result
}

from ojg.

ohler55 avatar ohler55 commented on June 2, 2024

Thanks, that helps.

from ojg.

ohler55 avatar ohler55 commented on June 2, 2024

I found the problem. query2 is checking for @type being equal to "type1". There is no match for that in the json doc. There is a match for "attype1" though. This worked for me:

func TestFilterAt(t *testing.T) {
	jsondoc := `{
			"item1": {
				"id": "item1",
				"type": "type1",
				"@type": "attype1"
			},
			"item2": {
				"id": "item2",
				"type": "type2",
				"@type": "attype2"
			}
		}`
	store := oj.MustParseString(jsondoc)
	x := jp.MustParseString(`$[?(@['@type']=="attype1")]`)
	result := x.Get(store)
	tt.Equal(t, 1, len(result))
}

That test has been added to the jp/get_test.go file in the project.

from ojg.

hspaay avatar hspaay commented on June 2, 2024

Arggh, that was a silly mistake with my test cases.
Sorry for wasting your time and thank you for looking into it.

ps: thank you again for a great library!

from ojg.

ohler55 avatar ohler55 commented on June 2, 2024

All good. It lets me know there are some users out there. :-)

from ojg.

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.