Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

query for first nesting level #4

Open
Chutchev opened this issue Apr 27, 2023 · 6 comments
Open

query for first nesting level #4

Chutchev opened this issue Apr 27, 2023 · 6 comments

Comments

@Chutchev
Copy link

Hi, @volans-
I have json like this:

{
    "id": "uuid4",
    "vendor": {
        "id": "vendor_uuid4",
        "name": "vendor_name",
        "slug": "vendor_slug"
    },
    "vendor_id": "vendor_uuid4",
    "name": "course_name",
    "slug": "course_slug",
    "status": "course_status"
} 

status can have values such as: published, archived. I want to check that this course have status equals published, but I can't create query to status field. I didn't find similar examples In original library documentations. How can I do it?

@volans-
Copy link
Owner

volans- commented Apr 27, 2023

@Chutchev what do you mean exactly by I want to check that this course have status equals published? What kind of object you'd want as a result of the check?
Is this a single object or do you have a list of objects from which you want to extract only the ones with a specific status?

@Chutchev
Copy link
Author

this is a single object.
Result should be this object if status=="published"

@volans-
Copy link
Owner

volans- commented Apr 27, 2023

Ok, then in order to use the query capabilities you have to convert this to a list/array and query for the first object with status=="published". To create a list of the whole current JSON you can use the @this modifier.

So putting all together that would be:

[@this].#(status=="published")

If the status is published it will return the whole object. Let me know if that's what you needed.

@Chutchev
Copy link
Author

Wow, thank you. It works

@Chutchev
Copy link
Author

Chutchev commented Apr 27, 2023

Okay, does this mean that if I want to use a filter on the first level, I must always wrap the root in a list?
And if this json will be a list of subject, I would have to use an expression like this?

@this.#(status=="published")

@volans-
Copy link
Owner

volans- commented Apr 27, 2023

It's not the level, but the fact that queries works only on lists, see https://github.com/tidwall/gjson/blob/master/SYNTAX.md#queries

If you have already a list of objects you don't need the @this but can query them directly. With a JSON of:

[
  {
    "id": "uuid4_1",
    "vendor": {
        "id": "vendor_uuid4",
        "name": "vendor_name",
        "slug": "vendor_slug"
    },
    "vendor_id": "vendor_uuid4",
    "name": "course_name",
    "slug": "course_slug",
    "status": "published"
  }, 
  {
    "id": "uuid4_2",
    "vendor": {
        "id": "vendor_uuid4",
        "name": "vendor_name",
        "slug": "vendor_slug"
    },
    "vendor_id": "vendor_uuid4",
    "name": "course_name",
    "slug": "course_slug",
    "status": "archived"
  }
]

You can query the first object with a given status with:

#(status=="published")

or all the objects with a given status with:

#(status=="published")#

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants