How to create JSON-Code

Hi,
Can someone help me how I create the attached JSON-Code? I know I have to do it with a struct, but don't know how to do it exactly with the brackets.
{
"query": "Who likes Pizza?",
"documents": [
{
"text": "Andreas likes Pizza."
},
{
"docx": "b64;base64EncodededWordDocument"
}
]
}

Respuestas (1)

the cyclist
the cyclist el 7 de Abr. de 2023

0 votos

Will the jsonencode function do what you need?
If not, can you be more specific about what you have as input, and what exactly you want for output?

3 comentarios

Christopher Dirks
Christopher Dirks el 7 de Abr. de 2023
Editada: Christopher Dirks el 7 de Abr. de 2023
The jsonencode function is the right one. But I don't know how to give the information to the function, to get the exact structure of the information in JSON
I don't know if this is exactly what you need, but I think it should give you an idea of how to create a nested structure, and then convert it to JSON:
s(1).Query = 'View from the 15th Floor';
s(1).Documents.text = "Andreas";
s(1).Documents.docx = "b64;base64";
s(2).Query = 'View from the 31st Floor';
s(2).Documents.text = "Joe";
s(2).Documents.docx = "b32;base32";
j = jsonencode(s,PrettyPrint=true)
j =
'[ { "Query": "View from the 15th Floor", "Documents": { "text": "Andreas", "docx": "b64;base64" } }, { "Query": "View from the 31st Floor", "Documents": { "text": "Joe", "docx": "b32;base32" } } ]'
Christopher Dirks
Christopher Dirks el 9 de Abr. de 2023
I think this is the right way, but not the solution. I get this error:
The server returned the status 400 with message "Bad Request" in response to the request to URL https://api.aleph-alpha.com/qa.
byteArray = copyContentToByteArray(connection);
Error in webwrite (line 139)
[varargout{1:nargout}] = readContentFromWebService(connection, options);
I think I have to use the square brackets, but still do not know how to.

Iniciar sesión para comentar.

Productos

Versión

R2023a

Etiquetas

Preguntada:

el 7 de Abr. de 2023

Comentada:

el 9 de Abr. de 2023

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by