How to search value in nested array JSON?
Hi, I want to search a value inside a jsonb that stored in PostgreSQL
Here the jsonb example inside the PostgreSQL:
[
{
"INFO": {
"DESCRIPTION": "lorem ipsum",
"BUYERS": [
{
"ID": 1,
"NAME": "John Doe"
},
{
"ID": 2,
"NAME": "Jane Doe"
}
]
}
},
{
"INFO": {
"DESCRIPTION": "lorem ipsum",
"BUYERS": [
{
"ID": 1,
"NAME": "John Doe"
},
{
"ID": 2,
"NAME": "Michael"
}
]
}
},
]
From the JSONB that stored in the PostgreSQL I want search the product by the buyers name if any of them are match.
So the PostgreSQL query that worked would be:
SELECT * FROM products WHERE info -> 'buyers' @> '[{"name": "Jane Doe"}]'
I have tried with GORM by using named arguments but it doesn't work:
result = db.Where("info-> 'buyers' @> '[{\"name\": @name}]'", map[string]interface{}{"name": request.body.name}).Find(&products)
Where I got error and the SQL query output like below:
SELECT * FROM "individu_table_test_01" WHERE cif -> 'users' @> '[{"cif": @cif}]'::jsonb
ERROR: invalid input syntax for type json (SQLSTATE 22P02)
The document you expected this should be explained
Expected answer
What I want is the product result that has buyer's name 'Jane Doe' based on the given search name, like this:
[
{
"INFO": {
"DESCRIPTION": "lorem ipsum",
"BUYERS": [
{
"ID": 1,
"NAME": "John Doe"
},
{
"ID": 2,
"NAME": "Jane Doe"
}
]
}
}
]
Comment From: a631807682
Please describe it in go-gorm/playground
Comment From: a631807682
I'm sorry my description is wrong, what I really mean is, checkout go-gorm/playground and create a pull request to let us know which part of the code is having problems like you said.
Comment From: github-actions[bot]
This issue has been automatically marked as stale because it has been open 360 days with no activity. Remove stale label or comment or this will be closed in 180 days
Comment From: a631807682
https://github.com/go-gorm/datatypes#json