Hi Günter, have you considered using 
    json:parse 

instead? I find the W3C-defined format obtained from fn:json-to-xml unnatural 
and unpractical; whereas BaseX-defined json:parse produces a very elegant, 
intuitive and natural representation of the content - turning JSON documents 
into near-equivalents of XML documents. Consider this example: given the json 
shown at the end of this post, getting a simple report (list of flight 
destinations and times) using json:parse might looks like this:
unparsed-text('flights.json') ! json:parse(.) 
// flight[departureAirport = 'CGN']/concat(arrivalAirport, ' ', departureTime)

In comparison, using fn:json-to-xml, my query looks like this:
unparsed-text('flights.json') ! json-to-xml(.) 
// *:map[@key eq 'flight'][*:string[@key = 'departureAirport'] = 'CGN']
/concat(*:string[@key = 'arrivalAirport'], ' ', *:string[@key = 
'departureTime'])

Kind regards,Hans-Jürgen

[
  {
    "flight" : {
    "departureAirport" : "CGN",
    "arrivalAirport" : "JFK",
    "departureTime" : "2017-08-26T20:00:00"
    }
  },
  {
    "flight" : {
      "departureAirport" : "CGN",
      "arrivalAirport" : "FRA",
      "departureTime" : "2017-08-26T21:00:00"
    }
  }
]



    Günter Dunz-Wolff <guenter.dunzwo...@gmail.com> schrieb am 12:36 Samstag, 
26.August 2017:
 

 It works. I’ll go with the second solution. Martin, thanks a lot!

> Am 26.08.2017 um 12:26 schrieb Martin Honnen <martin.hon...@gmx.de>:
> 
> On 26.08.2017 12:23, Günter Dunz-Wolff wrote:
> 
>> I’m trying to get data out of json via xquery.
>> This is my scenario:
>> 1. Fetching data with fetch:text() calling a REST-API
>> 2. Transforming the data with json-to-xml, so I’ll get something like
>>     <array xmlns="http://www.w3.org/2005/xpath-functions“>
>>         <map>
>>             <string key=„word1“>gehen</string>
>>             ...
>>         </map>
>>         ...
>>     </array>
>> 3. In my xqm-file I have: let $json_xml := json-to-xml(my_fetched_json)
>> 4. I try to get „gehen“ with $json_xml//string[@key='word1‘], but it yields 
>> nothing, also $json_xml//string yields nothing. Only $json_xml//text() 
>> yields all string-values into a long string.
>> What am I doing wrong? Or is there another way, to work with JSON-Data to 
>> get values of a given key?
> 
> The elements are in the namespace http://www.w3.org/2005/xpath-functions so 
> either use
>  $json_xml//*:string[@key='word1']
> or make sure you declare a prefix (e.g. 'fn') for the namespace and use that 
> prefix in e.g.
>  $json_xml//fn:string[@key='word1']
> 


   

Reply via email to