JSON Validation Showing as Duplicate Object warnings - json

I am new to Json, but I tried below json request.
{
"recipes": [{
"mr_id": 1,
"ingredients": [{
"ingredient_id": 2,
"quantity": 4,
"unit": "g",
"nutrients": [{
"nutrient_id": 1,
"quantity": 2,
"unit": "g"
}],
"ingredient_id": 3,
"quantity": 4,
"unit": "g",
"nutrients": [{
"nutrient_id": 2,
"quantity": 2,
"unit": "g"
}]
}],
"mr_id": 2,
"ingredients": [{
"ingredient_id": 4,
"quantity": 4,
"unit": "g",
"nutrients": [{
"nutrient_id": 3,
"quantity": 2,
"unit": "g"
}],
"ingredient_id": 5,
"quantity": 4,
"unit": "g",
"nutrients": [{
"nutrient_id": 4,
"quantity": 2,
"unit": "g"
}]
}]
}]
}
But when i place this in POSTMAN I am getting duplicate Object keys error , Even i checked in online jsonvalidator websites. Showing same duplicate warnings. Can some one please correct this?

Your json is not valid. Objects in your json are not properly separated, it causes double object property name error. Each object (outer and nested) should be included into brackets {}. This way each mr_id and each ingridient_id would be in the different objects, now they are in the same one.
{
"recipes": [
{
"mr_id": 1,
"ingredients": [
{
"ingredient_id": 2,
"quantity": 4,
"unit": "g",
"nutrients": [{
"nutrient_id": 1,
"quantity": 2,
"unit": "g"
}]
},
{
"ingredient_id": 3,
"quantity": 4,
"unit": "g",
"nutrients": [{
"nutrient_id": 2,
"quantity": 2,
"unit": "g"
}]
}
]
},
{
"mr_id": 2,
"ingredients": [{
"ingredient_id": 4,
"quantity": 4,
"unit": "g",
"nutrients": [{
"nutrient_id": 3,
"quantity": 2,
"unit": "g"
}]
},
{
"ingredient_id": 5,
"quantity": 4,
"unit": "g",
"nutrients": [{
"nutrient_id": 4,
"quantity": 2,
"unit": "g"
}]
}
]
}
]
}

Related

Having problems entering JSON into MySQL database

I am trying to develop a system to handle data from a third party site. I will enter their data in my MySQL DB. Part of their data is a JSON string. I am new to JSON in MySQL. I have read and watched a tutorial on how to query JSON. I have done so successfully with JSON strings that I have entered. The problem is I cannot get their string to insert into my table. I get an error that says,"#3140 - Invalid JSON text: "The document root must not follow by other values." at position 11 in value for column 'ordered_shed.items'. Below is the JSON I received from the company. I have looked at it and cannot figure out what the problem is. I am using PHPMyAdmin to insert the code. The structure is.
id int(11) AUTO_INCREMENT
items JSON
"lineItems":[
{ "description": "details-item-size",
"productKey": "size",
"quantity": 1
},
{ "description": "details-item-style",
"model": "",
"price": 2229,
"productKey": "style",
"quantity": 1,
"value": ""
},
{ "description": "details-item-flooring",
"price": 0,
"productCategory": "flooring-interior",
"quantity": 1
},
{ "description": "details-item-floor-joist",
"productCategory": "flooring-interior",
"quantity": 1
},
{ "description": "details-item-roof-overhang",
"productKey": "RoofOverhang",
"quantity": 1
},
{ "description": "details-item-sidewall-height",
"productKey": "wall-height",
"quantity": 1
},
{ "description": "standard",
"productCategory": "structure",
"quantity": 1
},
{ "description": "Pressure Treated Skids",
"productCategory": "structure",
"quantity": 1
},
{ "description": "details-item-loft",
"productCategory": "flooring-interior",
"quantity": 1
},
{ "description": "details-item-roof-material",
"productKey": "roof-material",
"quantity": 1
},
{ "description": "details-item-siding-color",
"quantity": 1,
"productKey": "siding-color"
},
{ "description": "details-item-siding",
"productKey": "siding",
"quantity": 1
},
{ "description": "details-item-roof-color",
"quantity": 1,
"productKey": "roof-color"
},
{ "description": "details-item-trim-color",
"quantity": 1,
"productKey": "trim-color"
}]
Any help would be greatly appreciated.
The JSON you've provided is not valid. You can always check here.
I think you just need your JSON to look like this:
{
"lineItems": [{
"description": "details-item-size",
"productKey": "size",
"quantity": 1
},
{
"description": "details-item-style",
"model": "",
"price": 2229,
"productKey": "style",
"quantity": 1,
"value": ""
},
{
"description": "details-item-flooring",
"price": 0,
"productCategory": "flooring-interior",
"quantity": 1
},
{
"description": "details-item-floor-joist",
"productCategory": "flooring-interior",
"quantity": 1
},
{
"description": "details-item-roof-overhang",
"productKey": "RoofOverhang",
"quantity": 1
},
{
"description": "details-item-sidewall-height",
"productKey": "wall-height",
"quantity": 1
},
{
"description": "standard",
"productCategory": "structure",
"quantity": 1
},
{
"description": "Pressure Treated Skids",
"productCategory": "structure",
"quantity": 1
},
{
"description": "details-item-loft",
"productCategory": "flooring-interior",
"quantity": 1
},
{
"description": "details-item-roof-material",
"productKey": "roof-material",
"quantity": 1
},
{
"description": "details-item-siding-color",
"quantity": 1,
"productKey": "siding-color"
},
{
"description": "details-item-siding",
"productKey": "siding",
"quantity": 1
},
{
"description": "details-item-roof-color",
"quantity": 1,
"productKey": "roof-color"
},
{
"description": "details-item-trim-color",
"quantity": 1,
"productKey": "trim-color"
}
]
}
You can use
SELECT json_valid(<jscol>) as "isValid?"
to check whether json column is valid. If returns 1 then it's valid. Invalid if it returns 0 (zero), and you can insert like this :
insert into tab(items)
select case when json_valid(<jscol>)=1 then
<jscol>
end;
Demo

How produces statistics with laravel?

I work on a laravel application that is a bit like a quiz. You create an exercise, you add questions and each question to answers.
Until then everything is fine.
My interest is to present statistics for each exercise after schoolchildren have answered the tests.
I want to be able to display the answer percentage for each question.
Here is an example of the data structure I have:
[
{
"id": 1,
"exercice_id": 1,
"question": "Lorem ipsum ?",
"responses": [
{
"id": 1,
"exercice_id": 1,
"question_id": 1,
"response_text": "Yes",
},
{
"id": 2,
"exercice_id": 1,
"question_id": 1,
"response_text": "No",
}
],
"choice": [
{
"id": 1,
"exercice_id": 1,
"question_id": 1,
"response_id": 1,
},
{
"id": 2,
"exercice_id": 1,
"question_id": 1,
"response_id": 1,
},
{
"id": 3,
"exercice_id": 1,
"question_id": 1,
"response_id": 2,
}
]
},
{
"id": 2,
"exercice_id": 1,
"question": "fake text ?",
"responses": [
{
"id": 3,
"exercice_id": 1,
"question_id": 2,
"response_text": "A",
},
{
"id": 4,
"exercice_id": 1,
"question_id": 2,
"response_text": "B",
},
{
"id": 5,
"exercice_id": 1,
"question_id": 2,
"response_text": "C",
}
],
"choice": [
{
"id": 4,
"exercice_id": 1,
"question_id": 2,
"response_id": 5,
},
{
"id": 5,
"exercice_id": 1,
"question_id": 2,
"response_id": 3,
}
]
}
]
I tried the groupBy method on several elements. but I have not found the formula yet.
return response()->json(Question::with('responses','choice')
->where('exercice_id',$exo->id)
//->groupBy('choice')
->get(),
200,[], JSON_NUMERIC_CHECK);

Export mysql data to JSON in a nested and recursivelty way starting from a table?

I have seen several examples to export MySQL tables to JSON, however such examples export data in an aggregated way. For example, take a database where you have two tables "Invoice head" and "Invoice details". "Invoice details" is a child of "Invoice head".The data in JSON is usually represented like:
{
"invoice_head": [
{
"number": 1,
"cliente": "Carlos",
"date": "2016-12-12"
},
{
"number": 2,
"cliente": "Fernando",
"date": "2017-01-01"
}
],
"invoice_details": [
{
"headnumber": 1,
"lineno": 1,
"product": "Shoes",
"quantity": 2
},
{
"headnumber": 1,
"lineno": 2,
"product": "Socks",
"quantity": 1
},
{
"headnumber": 2,
"lineno": 1,
"product": "Laptop",
"quantity": 1
}
],
}
I need to export data in a nested way:
{
"invice_head": [
{
"number": 1,
"cliente": "Carlos",
"date": "2016-12-12",
"invice_details": [
{
"headnumber": 1,
"lineno": 1,
"product": "Shoes",
"quantity": 2
},
{
"headnumber": 1,
"lineno": 2,
"product": "Socks",
"quantity": 1
}
]
},
{
"number": 2,
"cliente": "Fernando",
"date": "2017-01-01"
"invice_details": [
{
"headnumber": 2,
"lineno": 1,
"product": "Laptop",
"quantity": 1
}
]
}
]
}
For this I guess one needs to start in a table and recursively go through its childs for each record.
Does anybody knows if there is anything that does it? I don't want to reinvent the wheel.

Querying JSONB using Postgres

I am attempting to get an element in my JSON with a query.
I am using Groovy, Postgres 9.4 and JSONB.
Here is my JSON
{
"id": "${ID}",
"team": {
"id": "123",
"name": "Shire Soldiers"
},
"playersContainer": {
"series": [
{
"id": "1",
"name": "Nick",
"teamName": "Shire Soldiers",
"ratings": [
1,
5,
6,
9
],
"assists": 17,
"manOfTheMatches": 20,
"cleanSheets": 1,
"data": [
3,
2,
3,
5,
6
],
"totalGoals": 19
},
{
"id": "2",
"name": "Pasty",
"teamName": "Shire Soldiers",
"ratings": [
6,
8,
9,
10
],
"assists": 25,
"manOfTheMatches": 32,
"cleanSheets": 2,
"data": [
3,
5,
7,
9,
10
],
"totalGoals": 24
}
]
}
}
I want to fetch the individual elements in the series array by their ID, I am currently using this query below
select content->'playersContainer'->'series' from site_content
where content->'playersContainer'->'series' #> '[{"id":"1"}]';
However this brings me back me back both the element with an id of 1 and 2
Below is what I get back
"[{"id": "1", "data": [3, 2, 3, 5, 6], "name": "Nick", "assists": 17, "ratings": [1, 5, 6, 9], "teamName": "Shire Soldiers", "totalGoals": 19, "cleanSheets": 1, "manOfTheMatches": 20}, {"id": "2", "data": [3, 5, 7, 9, 10], "name": "Pasty", "assists": 25, "r (...)"
Can anyone see where I am going wrong? I have seen some other questions on here but they don't help with this.
content->'playersContainer'->'series' is an array. Use jsonb_array_elements() if you want to find a specific element in an array.
select elem
from site_content,
lateral jsonb_array_elements(content->'playersContainer'->'series') elem
where elem #> '{"id":"1"}';
Test it here.

find values of json array using postgresql query

This is my JSON value
"order": {
"items": [{
"amount": 30000,
"product_name": "product A",
"quantity": 3,
}, {
"amount": 1000,
"product_name": "product B",
"quantity": 1,
}],
"currency": "USD"
}
I want get values of amount from the array using mysql queries. I tried select json_extract_path_text('order','items') but it fetches whole value
[{
"amount": 30000,
"product_name": "product A",
"quantity": 3,
}, {
"amount": 1000,
"product_name": "product B",
"quantity": 1,
}]
Can you help me?
You can use json_array_elements() function:
select
(value->>'amount')::int as amount
from json_array_elements(
'{"order":
{ "items": [
{"amount": 30000,"product_name": "product A", "quantity": 3},
{"amount": 1000,"product_name": "product B","quantity": 1}
],
"currency": "USD"}
}'::json->'order'->'items'
)