I need to create a stored procedure in mysql that take some fields from DB and create a neasted json object:
Firstly I create a json object as showing below:
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": []
}
And secondly a json array object like this:
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
And the result query showld be something like this:
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": {[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
}
I got an error when I'm trying to insert a nested json array into a json object
I joined JSON_OBJECT with json_arrayagg as below:
{
"name": "Moon",
"resume": "This is a resume.",
"dt_ini": "2018-09-01",
"dt_end": null,
"cases": [
{
"unit": "unit 1",
"unit_id": 10
},
{
"unit": "unit 2",
"unit_id": 290
},
{
"unit": "unit 3",
"unit_id": 44
},
{
"unit": "unit 4",
"unit_id": 108
}
]
}
The final result is this:
CREATE DEFINER=`root`#`localhost` PROCEDURE `get_list_case`(
IN `code` int,
IN `base_code` int)
BEGIN
DECLARE json TEXT DEFAULT '';
SELECT JSON_OBJECT(
'name', v.name,
'dt_ini', v.dt_ini,
'dt_end', v.dt_end,
'resumo', v.resumo,
'cases', ( select json_arrayagg(json_object(
'unit_id',`tb_unit`.`unit_id`,
'unit',`tb_unit`.`unit`))
from tb_unit_case
INNER JOIN tb_unit ON tb_unit_case.unid_code = tb_unit.unit_id
WHERE tb_unit_case.case_code = code)
) INTO json
FROM v_case AS v
WHERE v.code = code and v.base_code = base_code;
SELECT json;
END
DECLARE json TEXT DEFAULT '';
DECLARE code INTEGER;
SET code = 1;
SELECT JSON_OBJECT(
'form', v.form_name,
'version', v.version,
'questions, ( select json_arrayagg(json_object(
'ordem',`tb_questions`.`order`,
'num_questions',`tb_questions`.`num`
'question1',`tb_questions`.`question1`
'answer1',`tb_questions`.`answer1`
))
from tb_questions
WHERE tb_questions.code = code)
) INTO json
FROM v_case AS v
WHERE v.code = code;
This is not an array, you have to remove {}
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
Related
I need to create a stored procedure in mysql that take some fields from DB and create a neasted json object:
Firstly I create a json object as showing below:
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": []
}
And secondly a json array object like this:
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
And the result query showld be something like this:
{
"form": "Exams tests",
"version": "v3.001.01",
"questions": {[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
}
I got an error when I'm trying to insert a nested json array into a json object
I joined JSON_OBJECT with json_arrayagg as below:
{
"name": "Moon",
"resume": "This is a resume.",
"dt_ini": "2018-09-01",
"dt_end": null,
"cases": [
{
"unit": "unit 1",
"unit_id": 10
},
{
"unit": "unit 2",
"unit_id": 290
},
{
"unit": "unit 3",
"unit_id": 44
},
{
"unit": "unit 4",
"unit_id": 108
}
]
}
The final result is this:
CREATE DEFINER=`root`#`localhost` PROCEDURE `get_list_case`(
IN `code` int,
IN `base_code` int)
BEGIN
DECLARE json TEXT DEFAULT '';
SELECT JSON_OBJECT(
'name', v.name,
'dt_ini', v.dt_ini,
'dt_end', v.dt_end,
'resumo', v.resumo,
'cases', ( select json_arrayagg(json_object(
'unit_id',`tb_unit`.`unit_id`,
'unit',`tb_unit`.`unit`))
from tb_unit_case
INNER JOIN tb_unit ON tb_unit_case.unid_code = tb_unit.unit_id
WHERE tb_unit_case.case_code = code)
) INTO json
FROM v_case AS v
WHERE v.code = code and v.base_code = base_code;
SELECT json;
END
DECLARE json TEXT DEFAULT '';
DECLARE code INTEGER;
SET code = 1;
SELECT JSON_OBJECT(
'form', v.form_name,
'version', v.version,
'questions, ( select json_arrayagg(json_object(
'ordem',`tb_questions`.`order`,
'num_questions',`tb_questions`.`num`
'question1',`tb_questions`.`question1`
'answer1',`tb_questions`.`answer1`
))
from tb_questions
WHERE tb_questions.code = code)
) INTO json
FROM v_case AS v
WHERE v.code = code;
This is not an array, you have to remove {}
{[
{
"ordem": 1,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
}
{
"ordem": 2,
"num_questions": 2,
"question1": "How old are you?"
"answer1": "I'm 18 years old."
"question2": "Where do you live?"
"answer2": "I live in Boston."
}
{
"ordem": 3,
"num_questions": 1,
"question1": "How old are you?"
"answer1": "I'm 23 years old."
}
]}
I have this JSON:
{
"ID": "42871",
"playerid": "226790",
"position": "CDM",
"resource_id": "50558438",
"playername": "Onyinye Ndidi",
"common_name": "Ndidi",
"club_name": "Leicester City",
"nation_name": "Nigeria",
"league_name": "Premier League",
"ps_LCPrice": "72000",
"pc_LCPrice": "85000",
"xbox_LCPrice": "81500"
},
{
"ID": "42871",
"playerid": "226790",
"position": "CDM",
"resource_id": "50558438",
"playername": "Kai Havertz",
"common_name": "Havertz",
"club_name": "Bayer 04 Leverkusen",
"nation_name": "Nigeria",
"league_name": "Premier League",
"ps_LCPrice": "44000",
"pc_LCPrice": "55000",
"xbox_LCPrice": "99000"
},
I want to get the output as this:
playername, xbox_LCPrice
playername, xbox_LCPrice
So it does the first array then the second one and keeps going to the end, how would I do that?
Try this
with open(config_file) as f:
config_dict = json.load(f)
player_name = config_dict['playername']
I'm trying to dynamically add an object inside every object which is present in my json array. But I'm unable to do so. My object is getting appended at the end of json which is not what I want.
jsonArray:any=[
{
"id": 1000,
"body": "some comment",
"postId": 1
},
{
"id": 2,
"body": "some comment",
"postId": 1
},
{
"id": 3,
"body": "some comment",
"postId": 1
}
]
selectFLag:any={"selected":"true"}
temArray:any;
learnJSONPArse()
{
for (var i = 0; this.jsonArray.length > i; i++)
{
Alert(this.jsonArray.length)
}
}
this.jsonArray.push(this.selectFLag)
-----expected output is
[
{
"id": 1000,
"body": "some comment",
"postId": 1,
"selected":"true"
},
{
"id": 2,
"body": "some comment",
"postId": 1,
"selected":"true"
},
{
"id": 3,
"body": "some comment",
"postId": 1,
"selected":"true"
}
]
You're question is a little unclear, but it sounds like you want to map each item in your array to a new item. The new item is the same as the old one, but assigned an additional property.
If so, something like this could work for you:
const objToAppend = { selected: true };
jsonArray.map(item => Object.assign(item, objToAppend));
Refs:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map
I'm using many to many relationships using pivot table. But the problem is when I'm returning json response, the json also contains the pivot attribute as shown below:
{
"id": 2,
"job_title": "et",
"job_description": "Iusto provident.",
"job_industry": "Braun, Jast and Quigley",
"job_location": "Christiansenland",
"job_experience": 7,
"employment_type": "full",
"recruiter_id": 9,
"status": 1,
"posted_date": "2016-02-02 07:55:28",
"skills": [
{
"id": 1,
"value": "molestiae",
"pivot": {
"job_id": 2,
"skill_id": 1
}
}
]
}
What I want is something like this:
{
"id": 2,
"job_title": "et",
"job_description": "Iusto provident.",
"job_industry": "Braun, Jast and Quigley",
"job_location": "Christiansenland",
"job_experience": 7,
"employment_type": "full",
"recruiter_id": 9,
"status": 1,
"posted_date": "2016-02-02 07:55:28",
"skills": [
{
"id": 1,
"value": "molestiae",
}
]
}
I have tried various solutions from the stackoverflow questions, but none of them seem to work. I'm new to Laravel. If you guys need more info on the model,I can post it. Please help.
Go to your Skill model and set:
protected $visible = ['id', 'value'];
I am writing a JSON file, but I am not sure about which of the following formats is the correct one?
Quoting variable names and all string values
{
"class": {
"number": 2,
"student": {
"name": "Tom",
"age": 1
},
"student": {
"name": "May",
"age": 2
}
}
}
or
Quoting only string values
{
class: {
number: 2,
student: {
name: "Tom",
age: 1
},
student:
{
name: "May",
age: 2
}
}
}
The first is valid, if you're unaware you can validate your JSON output online pretty easily here: http://www.jsonlint.com/
JSON requires the quotes. See http://json.org for the specifications.
In particular, the string production is:
string
'"' characters '"'
Old question, but the OP's JSON (first construction) may have the proper syntax, but it's going to cause trouble because it repeats the key student.
import simplejson
data = '''{
"class": {
"number": 2,
"student": {
"name": "Tom",
"age": 1
},
"student": {
"name": "May",
"age": 2
}
}
}'''
data_in = simplejson.loads(data)
print(data_in)
Yields: {'class': {'number': 2, 'student': {'age': 2, 'name': 'May'}}}
Where unique keys student_1 and student_2:
import simplejson
data = '''{
"class": {
"number": 2,
"student_1": {
"name": "Tom",
"age": 1
},
"student_2": {
"name": "May",
"age": 2
}
}
}'''
data_in = simplejson.loads(data)
print(data_in)
Yields: {'class': {'student_1': {'age': 1, 'name': 'Tom'}, 'number': 2, 'student_2': {'age': 2, 'name': 'May'}}}
UPDATE:
Agree with #Tomas Hesse that an array is better form. It would look like this:
import simplejson
data = '''{
"class": {
"number": 2,
"students" : [
{ "name" : "Tom", "age" : 1 },
{ "name" : "May", "age" : 2 }
]
}
}'''
data_in = simplejson.loads(data)
print(data_in)