Re: [basex-talk] Request and JSON parsing
Hi Günter, 2. I'm getting a full Object (but I dont know, if its the raw JSON-Object > like above), but I am not able, to parse it, to get a list in html. > Could you share the result of your response with us (possibly shortened)? It would be interesting to learn something about the type of the response body. What do you get if you inspect:type($response[2]) ? let $data := json:parse($input, map { 'format': 'xquery' }) > > return map:for-each($data, function($k, $v) { > > $k || ': ' || string-join($v, ', ') > > }) > If you use json:parse($input) without a specific format, you’ll get an XML representation of the JSON data, which is usually simpler to postprocess. Hope this helps Christian
Re: [basex-talk] Request and JSON parsing
On 25/04/2024 21:58, Günter Dunz-Wolff wrote: Hi there, via RESTXQ I request a vectorstore. The response is an JSON-Object. For example: With the following curl: curl https://vector.io/query-data \ -H "Authorization: Bearer ABcFMGhetc" \ -d '{ "data": "Du hast dem Menschen ein so freies, herrliches und üppiges Leben bestimmt", "topK": 2, "includeVectors": false, "includeMetadata": true }' I'm getting this object: { "result" : [ { "id" : "ba02277", "score" : 0.9949091, "metadata" : {"genre":"ba","blatt":"1810-01","autor":"Kleist","titel":"Zoroaster.","link":"https://kleist-digital.de/etc","text":"freies, herrliches und üppiges Leben bestimmt."} }, { "id" : "ba01711", "score" : 0.8891792, "metadata" : {"genre":"ba","blatt":"1810-29","autor":"Ompteda","titel":"Fragmente","link":"https://kleist-digital.de/etc","text":"So mit dem Menschen im Moralischen."} } ] } Score: result score Blatt: result blatt Autor: result autor Titel: result titel Text: result text Link: result link etc. Parse the result (if it is not parsed and you have it as JSON string) with parse-json($result)) then run XQuery like { parse-json($result)?result?*!(let $score := ?score return ?metadata ! Score: {$score} Blatt: {?blatt} Autor: {?autor} Titel: {?titel} Text: {?text} Link: {?link} ) } and it should give you something like Score: 0.9949091 Blatt: 1810-01 Autor: Kleist Titel: Zoroaster. Text: freies, herrliches und üppiges Leben bestimmt. Link: https://kleist-digital.de/etc Score: 0.8891792 Blatt: 1810-29 Autor: Ompteda Titel: Fragmente Text: So mit dem Menschen im Moralischen. Link: https://kleist-digital.de/etc It is not quite the result you showed but I have left out the outer `li` elements as an li inside a div doesn't make sense Fiddle https://bxfiddle.cloud.basexgmbh.de/?share=%28%27query%21%27%3Cdiv%3EH%28H%3Fresult%3F*%27%3A%7Blet+%24V+%3A%3D+%3FV+return+%3Fmetadata+%27_%3CulUScore5%24V3BlattYblatt3AutorYautor3TiwlYtiwl3TextYwxt3LinkYlink%29IJIli%3EHHIul%3EH%7DH%29HIdiv%3E%27%7Econwxt%21%27%28C6resultG%5BW2277z9949091%2CCE-01NautorRKleistNtiwlRZoroaswr.ND6freies%2C+herrliches+und+%C3%BCppiges+Leben+bestimmt.6%29C%29%2CW1711z8891792%2CCE-29NautorROmpwdaNtiwlRFragmenwND6So+mit+dem+Menschen+im+Moralischen.6%29C%29+%5DHH%29%27%7Emode%21%27XQuery+%7BBaseX%7D%27%7EconwxtType%21%27json%27%293%29IJIliU5_IJ+%3CJ%286%5C%27CHHPDlinkRhttps%3A%2F%2Fkleist-digital.de%2FetcNwxt%5C%21EP6metadataG%286genreRbaNblattR1810G6+_H%5CnI%3C%2FJspan%3EN6%2C6P++R%5C%216U%3EHH%5Ct%3Cli%3E%3CJVscoreW+%28CP6idG6ba0Y5%3F_%3A+wtez6%2CCP6VG0.%01zw_YWVURPNJIHGEDC653_
[basex-talk] Request and JSON parsing
Hi there, via RESTXQ I request a vectorstore. The response is an JSON-Object. For example: With the following curl: curl https://vector.io/query-data \ -H "Authorization: Bearer ABcFMGhetc" \ -d '{ "data": "Du hast dem Menschen ein so freies, herrliches und üppiges Leben bestimmt", "topK": 2, "includeVectors": false, "includeMetadata": true }' I'm getting this object: { "result" : [ { "id" : "ba02277", "score" : 0.9949091, "metadata" : {"genre":"ba","blatt":"1810-01","autor":"Kleist","titel":"Zoroaster.","link":"https://kleist-digital.de/etc","text":"freies, herrliches und üppiges Leben bestimmt."} }, { "id" : "ba01711", "score" : 0.8891792, "metadata" : {"genre":"ba","blatt":"1810-29","autor":"Ompteda","titel":"Fragmente","link":"https://kleist-digital.de/etc","text":"So mit dem Menschen im Moralischen."} } ] } 1. Is the request in my RESTXQ in the right form? http:send-request( {{ "data": "{$query}", "topK": 20, "includeVectors": false, "includeMetadata": true }} ) 2. I'm getting a full Object (but I dont know, if its the raw JSON-Object like above), but I am not able, to parse it, to get a list in html. In the documentation https://docs.basex.org/wiki/JSON_Module is an example: let $input := '{ "Title": "Drinks", "Author": [ "Jim Daniels", "Jack Beam" ] }' let $data := json:parse($input, map { 'format': 'xquery' }) return map:for-each($data, function($k, $v) { $k || ': ' || string-join($v, ', ') }) But my Object is more complex and I don't know, how to get two and more datasets with $k and $v out of the object. I want to have the following list: Score: result score Blatt: result blatt Autor: result autor Titel: result titel Text: result text Link: result link etc. Thanks for any help Günter