I have a JSON data set containing exercise information that I want to insert into a MySQL database.
Each exercise has properties such as name, force, level, etc.
I need to convert this JSON data into MySQL INSERT statements to add the data to a table, as well as a CREATE TABLE statement to define the table structure.
Here is an example snippet of the JSON data:
"exercises": [
{
"name": "3/4 Sit-Up",
"force": "pull",
"level": "beginner",
"mechanic": "compound",
"equipment": "body only",
"primaryMuscles": [
"abdominals"
],
"secondaryMuscles": [],
"instructions": [
"Lie down on the floor and secure your feet. Your legs should be bent at the knees.",
"Place your hands behind or to the side of your head. You will begin with your back on the ground. This will be your starting position.",
"Flex your hips and spine to raise your torso toward your knees.",
"At the top of the contraction your torso should be perpendicular to the ground. Reverse the motion, going only ¾ of the way down.",
"Repeat for the recommended amount of repetitions."
],
"category": "strength"
},
{
"name": "90/90 Hamstring",
"force": "push",
"level": "beginner",
"mechanic": null,
"equipment": "body only",
"primaryMuscles": [
"hamstrings"
],
"secondaryMuscles": [
"calves"
],
"instructions": [
"Lie on your back, with one leg extended straight out.",
"With the other leg, bend the hip and knee to 90 degrees. You may brace your leg with your hands if necessary. This will be your starting position.",
"Extend your leg straight into the air, pausing briefly at the top. Return the leg to the starting position.",
"Repeat for 10-20 repetitions, and then switch to the other leg."
],
"category": "stretching"
},
{
"name": "Ab Crunch Machine",
"force": "pull",
"level": "intermediate",
"mechanic": "isolation",
"equipment": "machine",
"primaryMuscles": [
"abdominals"
],
"secondaryMuscles": [],
"instructions": [
"Select a light resistance and sit down on the ab machine placing your feet under the pads provided and grabbing the top handles. Your arms should be bent at a 90 degree angle as you rest the triceps on the pads provided. This will be your starting position.",
"At the same time, begin to lift the legs up as you crunch your upper torso. Breathe out as you perform this movement. Tip: Be sure to use a slow and controlled motion. Concentrate on using your abs to move the weight while relaxing your legs and feet.",
"After a second pause, slowly return to the starting position as you breathe in.",
"Repeat the movement for the prescribed amount of repetitions."
],
"category": "strength"
}]
Can anyone provide guidance on how to write a script or SQL queries to achieve this conversion effectively?
Thank you!
2
Answers
Guys here's how I solved with python script. Firstly create db and table that fits your json data.
To achieve the conversion using SQL instead of Python, you can create the table and generate the INSERT statements directly in SQL.
In this SQL script:
The CREATE TABLE statement creates a table named exercises with columns corresponding to the properties of the exercises.
The INSERT INTO statements insert each exercise’s data into the exercises table.
You can execute this SQL script in your MySQL database to create the table and insert the data. Adjust the data in the INSERT INTO statements as needed for your specific JSON data.