sqloracle-databaseget-request

How do I write the correct request to output information to "get"? SQL


I immediately apologize for my English to you. The fact is that I need to write a get request for my application with recipes. I want to make the request look like this:

{
  "items": [
    {
      "id_recipe": 1,
      "ingredients": [
        {
          "name_ingredient": "Ingredient1",
          "counts": 30,
          "name_unit": "unit1"
        },
        {
          "name_ingredient": "Ingredient2 ",
          "counts": 1,
          "name_unit": "unit2 "
        },
        {
          "name_ingredient": "Ingredient3",
          "counts": 2,
          "name_unit": "unit1 "
        }
      ]
    },
    {
      "id_recipe": 2,
      "ingredients": [
        {
          "name_ingredient": "Ingredient2",
          "counts": 1,
          "name_unit": "unit3 "
        },
        {
          "name_ingredient": "Ingredient1",
          "counts": 400,
          "name_unit": "unit4"
        }
      ]
    }
  ]
}

But it looks like this

{
  "items": [
    {
      "id_recipe": 1,
      "name_ingredient": "Ingredient1",
      "counts": 30,
      "name_unit": "unit1"
    },
    {
      "id_recipe": 1,
      "name_ingredient": "Ingredient2 ",
      "counts": 1,
      "name_unit": "unit2 "
    },
    {
      "id_recipe": 1,
      "name_ingredient": "Ingredient3",
      "counts": 2,
      "name_unit": "unit1 "
    },
    {
      "id_recipe": 2,
      "name_ingredient": "Ingredient2",
      "counts": 1,
      "name_unit": "unit3 "
    },
    {
      "id_recipe": 2,
      "name_ingredient": "Ingredient1",
      "counts": 400,
      "name_unit": "unit4"
    }
  ]
}

That is, it is necessary to somehow combine elements with the same id_recept into an array. But I do not know how! Here's the code I'm using now:

SELECT PRODUCTS.ID_RECIPE, INGREDIENTS.NAME_INGREDIENT, PRODUCTS.COUNTS, UNITS_OF_MEASUREMENT.NAME_UNIT 
FROM PRODUCTS, INGREDIENTS, UNITS_OF_MEASUREMENT 
WHERE PRODUCTS.ID_INGREDIENT = INGREDIENTS.ID_INGREDIENT 
AND PRODUCTS.ID_MEASUREMENT = UNITS_OF_MEASUREMENT.ID_MEASUREMENT 
ORDER BY ID_RECIPE

This is how the table data looks like: table ingredients

table products

I use oracle. I will be glad if you can help!


Solution

  • gsalem was absolutely right. In my case it looks like this:

        select 'application/json', "JSON" from (SELECT JSON_OBJECT('items' value JSON_ARRAYAGG(
        JSON_OBJECT(
            'idRecipe' VALUE R.ID_RECIPE,
            'nameRecipe' VALUE R.NAME_RECIPE,
            'urlImage' VALUE R.URL_IMAGE,
            'descriptionText' VALUE R.DESCRIPTION_TEXT,
            'timeCooking' VALUE R.TIME_COOKING,
            'category' VALUE (
                SELECT JSON_ARRAYAGG(
                    JSON_OBJECT(
                        'idCategory' VALUE CAT.ID_CATEGORY,
                        'nameCategory' VALUE CAT.NAME_CATEGORY
                    ) RETURNING CLOB
                ) FROM CATEGORIES CAT WHERE R.ID_CATEGORY = CAT.ID_CATEGORY
            ),
            'userInfo' VALUE (
                SELECT JSON_ARRAYAGG(
                    JSON_OBJECT(
                        'idUser' VALUE U.ID_USER,
                        'nameUser' VALUE U.FULLNAME
                    ) RETURNING CLOB
                ) FROM USERS U WHERE R.ID_USER = U.ID_USER
            ),
            'rating' VALUE R.RATING,
            'products' VALUE (
                SELECT JSON_ARRAYAGG(
                    JSON_OBJECT(
                        'idIngredient' VALUE I.ID_INGREDIENT,
                        'nameImgredient' VALUE I.NAME_INGREDIENT,
                        'counts' VALUE P.COUNTS,
                        'measurement' VALUE (
                            SELECT JSON_ARRAYAGG(
                                JSON_OBJECT(
                                    'idMeasurement' VALUE M.ID_MEASUREMENT,
                                    'nameMeasurement' VALUE M.NAME_UNIT
                                ) RETURNING CLOB
                            ) FROM UNITS_OF_MEASUREMENT M WHERE P.ID_MEASUREMENT = M.ID_MEASUREMENT
                        )
                    ) RETURNING CLOB
                ) FROM PRODUCTS P, INGREDIENTS I WHERE P.ID_RECIPE = R.ID_RECIPE AND P.ID_INGREDIENT = I.ID_INGREDIENT
            ),
            'preparation' VALUE (
                SELECT JSON_ARRAYAGG(
                    JSON_OBJECT(
                        'step' VALUE PR.STEP,
                        'urlImage' VALUE PR.URL_IMAGE,
                        'description' VALUE PR.DESCRIPTIONS
                    )  RETURNING CLOB 
                ) FROM PREPARATION PR WHERE PR.ID_RECIPE = R.ID_RECIPE
            ) RETURNING CLOB
            ) ORDER BY ID_RECIPE RETURNING CLOB
        ) RETURNING CLOB
    ) AS "JSON" FROM RECIPES R)
    

    I explain why 'application/json' is needed. In the event that you want to use the RESTful service from oracle apex, in order to get a json file at the output, you need to add this to your code. Also select the source type as "media resource". In this case, everything will work.