0

I'm trying to convert query into a json file but it does not includes comma at the end of each row.

Query

SELECT json_object("NumeroDetallePedido",d.idpedido,"PrecioUnidad",d.preciounidad,"Cantidad",d.cantidad,"Descuento",d.descuento, "Producto",json_array("NombreProducto",pro.nombreProducto),"Pedidos",json_array("FechaPedido",pe.FechaPedido)) 
  FROM detallesdepedidos d
  JOIN productos pro 
    ON d.idproducto = pro.idproducto 
  JOIN pedidos pe 
    ON d.idpedido = pe.IdPedido 
  INTO OUTFILE '/var/lib/mysql-files/detallespedido_v4.json' 

Result {"Pedidos": ["FechaPedido", "1994-09-20"], "Cantidad": 45, "Producto": ["NombreProducto", "Té Dharamsala"], "Descuento": 0, "PrecioUnidad": 14, "NumeroDetallePedido": 10285} {"Pedidos": ["FechaPedido", "1994-09-30"], "Cantidad": 18, "Producto": ["NombreProducto", "Té Dharamsala"], "Descuento": 0, "PrecioUnidad": 14, "NumeroDetallePedido": 10294} {"Pedidos": ["FechaPedido", "1994-10-31"], "Cantidad": 20, "Producto": ["NombreProducto", "Té Dharamsala"], "Descuento": 0, "PrecioUnidad": 14, "NumeroDetallePedido": 10317}

it's not separated by commas ! Any ideas?

Thank u

2
  • where do you expect commas? Commented Feb 7, 2020 at 4:29
  • At the end of each row, the result it's making differents json_objects and I looking for an jsonArray with all the results Commented Feb 7, 2020 at 4:34

2 Answers 2

2

Please, try with below query using JSON_ARRAYAGG:

SELECT 
JSON_ARRAYAGG(
  json_object(
        "NumeroDetallePedido", d.idpedido, "PrecioUnidad", d.preciounidad, "Cantidad", d.cantidad, "Descuento", d.descuento, "Producto",
        json_array("NombreProducto", pro.nombreProducto), "Pedidos", json_array("FechaPedido",pe.FechaPedido)
    ) 
)
FROM detallesdepedidos d 
INNER JOIN productos pro ON d.idproducto = pro.idproducto 
INNER JOIN pedidos pe ON d.idpedido = pe.IdPedido)
Sign up to request clarification or add additional context in comments.

3 Comments

I had and error, FUNCTION neptuno.JSON_ARRAYAGG does not exist.
What's version of mysql.
MySQL 5.7.22 and later supports two aggregate JSON functions JSON_ARRAYAGG() and JSON_OBJECTAGG()
0

select concat('[', GROUP_CONCAT( JSON_OBJECT(
'name_field', name_field ,'address_field', address_field ,'contact_age', contact_age ) SEPARATOR ',') ,']') from contact;

I was looking for this.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.