2

I am trying to get all the values of json string as a single string. for example in xquery xml

let $x := <a> welcome to the world of <b> JSONiq </b></a>
return string($x)

will return welcome to the world of JSONiq

what is the equvalent result of of following documents in JSONiq:

let $y := {"a":"welcome to the world of ","b":" JSONiq"}
return xxxx($y)

The result should be same welcome to the world of JSONiq If you know in javascript also it would be great.

Gunther
  • 5,146
  • 1
  • 24
  • 35
Prakash Thapa
  • 1,285
  • 14
  • 27

2 Answers2

1

First you need to get all values, either with libjn:values or its definition, then you can use fn:string-join to get a single string:

So

declare namespace libjn = "http://jsoniq.org/function-library";
let $y := {"a":"welcome to the world of ","b":" JSONiq"}
return string-join(libjn:values($y) ! string(), "")

or

let $y := {"a":"welcome to the world of ","b":" JSONiq"}
return string-join($y() ! string($y(.)), "")

This might also return "JSONiqwelcome to the world of ", since the object keys are unordered

BeniBela
  • 16,412
  • 4
  • 45
  • 52
1

Find the recursive JSONiq script here for trying out. Just doing typeswitch and recursive calls does it:

declare function local:strings($v as item()) as xs:string*
{
  typeswitch ($v)
    case $object as object() return
      for $k in jn:keys($object)
        return local:strings($object($k))
    case $array as array() return
      for $member in jn:members($array)
        return local:strings($member)
    default return $v
};

let $y := {"a":"welcome to the world of", "b":" JSONiq", "x":{"d":"m"}}

return string-join(local:strings($y), "@")

The "@" is only for showing where the borders are, can be replaced by "":

welcome to the world of@JSONiq@m

Hermann.

HermannSW
  • 161
  • 1
  • 8