i have this json = '[
{
"id": 11,
"title": "1111",
},
{
"id": 22,
"title": "2222",
},
{
"id": 33,
"answer": "3333"
}
]'
I How to get all the object of the array in position 2 (or any)
Result string: '{
"id": 22,
"title": "2222",
}'
Thanks!
You could create your own function like this:
Function get_element(p_json VarChar2, p_which_one Number) Return VarChar2 IS
my_str VarChar2(255) := '';
start_pos Number := 0;
end_pos Number := 0;
Begin
start_pos := InStr(p_json, '{', 1, p_which_one);
end_pos := InStr(p_json, '}', 1, p_which_one) + 1;
my_str := my_str || SubStr(p_json, start_pos, end_pos - start_pos);
Return my_str;
End get_element;
and get the element that you want...
SET SERVEROUTPUT ON
Declare
json_str VarChar2(512) := '[ { "id": 11, "title": "1111", }, { "id": 22, "title": "2222", }, { "id": 33, "answer": "3333" }, { "id": 44, "title": "4444", }, { "id": 55, "title": "5555", }, { "id": 66, "answer": "6666" } ]';
my_str VarChar2(50) := '';
Begin
my_str := get_element(json_str, 2);
DBMS_OUTPUT.PUT_LINE(my_str);
End;
--
-- Result for p_which_one = 2 is
-- { "id": 22, "title": "2222", }
--
-- and for p_which_one = 4
-- { "id": 44, "title": "4444", }
... or you can loop them all...
SET SERVEROUTPUT ON
Declare
json_str VarChar2(512) := '[ { "id": 11, "title": "1111", }, { "id": 22, "title": "2222", }, { "id": 33, "answer": "3333" }, { "id": 44, "title": "4444", }, { "id": 55, "title": "5555", }, { "id": 66, "answer": "6666" } ]';
my_str VarChar2(255) := '';
Begin
For i in 1..100 Loop
if instr(json_str, '{', 1, i) > 0 Then
my_str := get_element(json_str, i);
DBMS_OUTPUT.PUT_LINE(my_str);
else
goto end_it;
end if;
End Loop;
<<end_it>>
null;
End;
/* R e s u l t :
anonymous block completed
{ "id": 11, "title": "1111", }
{ "id": 22, "title": "2222", }
{ "id": 33, "answer": "3333" }
{ "id": 44, "title": "4444", }
{ "id": 55, "title": "5555", }
{ "id": 66, "answer": "6666" }
*/
Related
So I have the following query done in Query builder on a GraphQL Query
return LearningPath::query()
->selectRaw('learning_paths.id,learning_paths.name, count(*) as completed_nodes')
->selectRaw('(SELECT count(*) FROM learning_path_levels WHERE learning_path_levels.learning_path_id = learning_paths.id) as total_levels')
->selectRaw('(SELECT count(*) FROM learning_path_level_nodes WHERE learning_path_level_nodes.learning_path_level_id = learning_path_levels.id) as total_nodes')
->selectRaw('(count(*) * 100) / (SELECT count(*) FROM learning_path_level_nodes WHERE learning_path_level_nodes.learning_path_level_id = learning_path_levels.id) as percentage')
->join('learning_path_levels', 'learning_path_levels.learning_path_id', '=', 'learning_paths.id')
->join('learning_path_level_nodes', 'learning_path_level_nodes.learning_path_level_id', '=', 'learning_path_levels.id')
->leftJoin('learning_path_node_users', 'learning_path_node_users.learning_path_level_node_id', '=', 'learning_path_level_nodes.id')
->where('learning_path_node_users.user_id', auth()->id())
->orWhereNull('learning_path_node_users.user_id')
->groupBy('learning_paths.id','learning_path_levels.id')
->get();
Which returns the following
{
"data": {
"learning_path_completed_levels": [
{
"id": "1",
"name": "Frontend",
"completed_nodes": 1,
"total_nodes": 3,
"percentage": 33
},
{
"id": "3",
"name": "Devops",
"completed_nodes": 5,
"total_nodes": 7,
"percentage": 71
},
{
"id": "6",
"name": "Project Manager",
"completed_nodes": 3,
"total_nodes": 6,
"percentage": 50
}
]
}
}
But this is not the correct result it should fetch, as it is returning me only the Learning Paths that have at least one level completed ( with the field is_successful marked as true )
It shoud query something like this:
{
"data": {
"learning_path_completed_levels": [
{
"id": "1",
"name": "Frontend",
"completed_nodes": 1,
"total_nodes": 3,
"percentage": 33
},
{
"id": "3",
"name": "Devops",
"completed_nodes": 1,
"total_nodes": 5,
"percentage": 20
},
{
"id": "4",
"name": "QA",
"completed_nodes": 0,
"total_nodes": 1,
"percentage": 0
},
{
"id": "5",
"name": "AI",
"completed_nodes": 0,
"total_nodes": 5,
"percentage": 0
},
{
"id": "6",
"name": "Project Manager",
"completed_nodes": 3,
"total_nodes": 6,
"percentage": 50
}
]
}
}
How can I modify my query so it returns the expected values?
I ran into the following problem:
I specified relationships in the data structures, but some data is output empty.
For example: I get data on a product, the price is pulled up, the stocks are pulled up, but the data on the store where they are located is not pulled up to the stock.
The structures that I use:
type Product struct {
Id uint `json:"id"`
Code int `json:"code" gorm:"index"`
ProductName string `json:"name"`
Brand string `json:"brand"`
Price Price `json:"price" gorm:"foreignKey:ProductCode;references:Code"`
Stocks []Stock `json:"stock" gorm:"foreignKey:ProductCode;references:Code"`
}
type Stock struct {
ProductCode int `json:"product_code"`
StoreCode uint `json:"store_code" gorm:"index;"`
Quantity int `json:"quantity"`
Store Store `json:"store" gorm:"foreignKey:StoreId;references:StoreCode;constraint:OnUpdate:CASCADE,OnDelete:CASCADE;"`
Timestamp int64 `json:"timestamp"`
}
type Price struct {
ProductCode int `json:"product_code"`
OldPrice int `json:"old_price"`
CurrentPrice int `json:"current_price"`
WholesalePrice int `json:"wholesale_price"`
Timestamp int64 `json:"timestamp"`
}
type Store struct {
Id uint `json:"id" gorm:"primaryKey"`
StoreKaspiId string `json:"store_kaspi_id"`
StoreId uint `json:"store_id"`
CityId uint `json:"city_id"`
City City `json:"city" gorm:"foreignKey:CityId"`
StoreAddress string `json:"store_address"`
}
type City struct {
Id uint `json:"id"`
Name string `json:"name"`
}
Query result:
{
"id": 79,
"code": 687,
"name": "Электромясорубка Аксион M21.03",
"brand": "Аксион",
"price": {
"product_code": 687,
"old_price": 30990,
"current_price": 23910,
"wholesale_price": 19900,
"timestamp": 1628824966063
},
"stock": [
{
"product_code": 687,
"store_code": 15,
"quantity": 37,
"store": {
"id": 0,
"store_kaspi_id": "",
"store_id": 0,
"city_id": 0,
"city": {
"id": 0,
"name": ""
},
"store_address": ""
},
"timestamp": 1628824966219
},
{
"product_code": 687,
"store_code": 20,
"quantity": 39,
"store": {
"id": 0,
"store_kaspi_id": "",
"store_id": 0,
"city_id": 0,
"city": {
"id": 0,
"name": ""
},
"store_address": ""
},
"timestamp": 1628824966219
},
{
"product_code": 687,
"store_code": 42,
"quantity": 39,
"store": {
"id": 0,
"store_kaspi_id": "",
"store_id": 0,
"city_id": 0,
"city": {
"id": 0,
"name": ""
},
"store_address": ""
},
"timestamp": 1628824966219
},
{
"product_code": 687,
"store_code": 45,
"quantity": 47,
"store": {
"id": 0,
"store_kaspi_id": "",
"store_id": 0,
"city_id": 0,
"city": {
"id": 0,
"name": ""
},
"store_address": ""
},
"timestamp": 1628824966219
}
]
}
Please help me understand!
Thank you in advance!
Question : How to retrieve Json data type data in SQL using a where condition in LARAVEL?
I want to display all the order that contains order->Product->user->id === 1
{
"currentUserID": 1,
"currentUserName": "Mohamed Naalir",
"order": [
{
"id": 26,
"Product": [
{
"id": 4,
"name": "Araliya Rice",
"desription": "Araliya Rice",
"salePrice": 500,
"category": "Rice",
"user": {
"id": 1,
"name": "Mohamed Naalir",
}
}
],
},
{
"id": 27,
"Product": [
{
"id": 2,
"name": "white sugar",
"desription": "aaa",
"salePrice": 100,
"category": "Sugar",
"user": {
"id": 5,
"name": "Mohamed Sharaf",
}
}
],
}
]
}
json where clauses
$orders = DB::table('orders')
->whereJsonContains('Product', [['user' => ['id' => 1]]])
->get();
I try to refrain from asking questions with simple answers but I can't seem to figure out what the issue is here... (Issue in title)
Relevant code:
match := new(Match)
if _, msgB, err = ws.ReadMessage(); err != nil {
panic(err)
}else {
println(string(msgB))
err = json.Unmarshal(msgB, match)
if err != nil { panic(err) }
}
type Match struct {
Teams [][]Char
Map [][]Tile
ID string //uuid
Socket *websocket.Conn `json:'-'`
}
type Char struct {
ID int
HP int
CT int
Stats statList
X int
Y int
ACList Actions
}
type statList struct {
Str int
Vit int
Int int
Wis int
Dex int
Spd int
}
type Actions struct {
Actions []Action
TICKCT int
}
String to unmarshal (Formatted for visibility):
{
"Teams": [
[
{
"ID": 1,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 0,
"Y": 0,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
],
[
{
"ID": 2,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 2,
"Y": 2,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
]
],
"Map": [
[
{
"Depth": 1,
"Type": 1,
"Unit": 1
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": 2
}
]
],
"ID": "0b055e19-9b96-e492-b816-43297f12cc39"}
Error:
2014/03/28 12:11:41 http: panic serving 127.0.0.1:56436: json: cannot
unmarshal number into Go value of type main.Char
I made a fixed version of the code (playground). This seemed to be the main mistake:
type Char struct {
ID int
HP int
CT int
Stats []int // This was statList which won't work
X int
Y int
ACList Actions
}
Also note the definition I made of Tile which allows numbers to be nil.
type Tile struct {
Depth int
Type int
Unit *int
}
You didn't provide all the structures so I made some up - probably wrong! All together that is:
import (
"encoding/json"
"fmt"
)
type Match struct {
Teams [][]Char
Map [][]Tile
ID string //uuid
// Socket *websocket.Conn `json:'-'`
}
type Char struct {
ID int
HP int
CT int
Stats []int // This was statList which won't work
X int
Y int
ACList Actions
}
type statList struct {
Str int
Vit int
Int int
Wis int
Dex int
Spd int
}
type Action string
type Actions struct {
Actions []Action
TICKCT int
}
type Tile struct {
Depth int
Type int
Unit *int
}
var data = `{
"Teams": [
[
{
"ID": 1,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 0,
"Y": 0,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
],
[
{
"ID": 2,
"HP": 10,
"CT": 0,
"Stats": [
1,
1,
1,
1,
1,
1
],
"X": 2,
"Y": 2,
"ACList": {
"Actions": [],
"TICKCT": 0
}
}
]
],
"Map": [
[
{
"Depth": 1,
"Type": 1,
"Unit": 1
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
}
],
[
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": null
},
{
"Depth": 1,
"Type": 1,
"Unit": 2
}
]
],
"ID": "0b055e19-9b96-e492-b816-43297f12cc39"}`
func main() {
match := new(Match)
err := json.Unmarshal([]byte(data), match)
if err != nil {
panic(err)
}
fmt.Printf("match = %#v\n", match)
}
My sample Json object is shown below:
{
"o": [
{
"level": 0,
"outlineItemId": 8,
"parentItemId": null,
"parentItem": null,
"order": 0,
"text": "section 1",
"isLeaf": "false",
"expanded": "true"
},
{
"level": 1,
"outlineItemId": 9,
"parentItemId": 8,
"parentItem": {
"level": 0,
"outlineItemId": 8,
"parentItemId": null,
"parentItem": null,
"order": 0,
"text": "section 1",
"isLeaf": "false",
"expanded": "true"
},
"order": 0,
"text": "sub 1",
"isLeaf": "false",
"expanded": "true"
},
{
"level": 2,
"outlineItemId": 10,
"parentItemId": 9,
"parentItem": {
"level": 1,
"outlineItemId": 9,
"parentItemId": 8,
"parentItem": {
"level": 0,
"outlineItemId": 8,
"parentItemId": null,
"parentItem": null,
"order": 0,
"text": "section 1",
"isLeaf": "false",
"expanded": "true"
},
"order": 0,
"text": "sub 1",
"isLeaf": "false",
"negateDevice": null,
"expanded": "true"
},
"order": 0,
"text": "sub sub 1",
"isLeaf": "true",
"expanded": "true"
}
]
}
Earlier when the tree was configured as:
treeReader: {
level_field: "level",
parent_id_field: "parentItemId",
leaf_field: "isLeaf",
expanded_field: "expanded"
},
I was displaying the correct indentation and image icons, however they were not expanded when the json obj always had "expanded":"true" so i tried the below code.
treeReader: {
level_field: "o.level",
parent_id_field: "o.parentItemId",
leaf_field: "o.isLeaf",
expanded_field: "o.expanded"
},
Now I am not getting the Image icons and the tree which was expanded earlier is now flat.
My Json reader just in case i goofed up..
jsonReader: {
root: 'o',
id: 'o.outlineItemId',
parentItemId: 'o.parentItem.outlineItemId',
text: 'o.text',
repeatitems: false,
page: function(obj) { return 1; },
total: function(obj) { return 1; },
records: function(obj) { return obj.o.length; },
},
Any help will be appreciated.
Shah
Got it!
For the reader i had to include cell:'' and remove the o. references. and also loaded:true in the json object.