Just as a heads-up, in MarkLogic 8 you will be able to use json
constructors, and will have to use xdmp:to-json-string instead:
xdmp:to-json-string(
object-node {
"x": 1,
"y": 1
}
)
Cheers
On 3/12/15, 3:02 AM, "Michael Blakeley" <[email protected]> wrote:
>I think Will is looking for a JSON syntax similar to map:new with
>map:entry args, which sometimes feels a little cleaner than calling
>map:map and map:put. As far as I know there's no direct equivalent for
>JSON.
>
>Until someone comes up with a better way, here are some utility functions
>that work with ML8. This approach probably trades away some performance
>for convenience, but it could have some value in a utility library.
>
>declare function local:json-entry($k as xs:string, $v as item()+)
>{
> element json:entry {
> element json:key { $k },
> $v ! element json:value { . } }
>};
>
>declare function local:json-new($entries as element(json:entry)*)
>{
> json:object(element json:object { $entries })
>};
>
>local:json-new(
> (local:json-entry('x', 1),
> local:json-entry('y', 2)))
>
>The code for json:value could and should be extended to add xsi:type
>attributes.
>
>Another way would be to xdmp:unquote a JSON string, but in that case you
>might as well just build the final JSON string yourself.
>
>-- Mike
>
>> On 11 Mar 2015, at 18:52 , Erik Hennum <[email protected]>
>>wrote:
>>
>> Hi, Will:
>>
>> You can use json:object() instead of map:map() for a mutable object
>>where order is important.
>>
>> I think the API is the same as that of map:map otherwise.
>>
>> JSON nodes, like XML nodes, are immutable.
>>
>> Erik Hennum
>>
>> ________________________________________
>> From: [email protected]
>>[[email protected]] on behalf of Will Thompson
>>[[email protected]]
>> Sent: Tuesday, March 10, 2015 5:09 PM
>> To: MarkLogic Developer Discussion
>> Subject: [MarkLogic Dev General] Constructing JSON objects
>>
>> Is there a nice way to construct a JSON object similar to how maps can
>>be constructed? Typically I would just do it this way:
>>
>> xdmp:to-json(
>> map:new((
>> map:entry('x', 1),
>> map:entry('y', 2)
>> )))
>>
>> Except that order is important in this specific scenario, and this
>>ruins ordering.
>>
>> -Will
>> _______________________________________________
>> General mailing list
>> [email protected]
>> http://developer.marklogic.com/mailman/listinfo/general
>> _______________________________________________
>> General mailing list
>> [email protected]
>> http://developer.marklogic.com/mailman/listinfo/general
>>
>
>_______________________________________________
>General mailing list
>[email protected]
>http://developer.marklogic.com/mailman/listinfo/general
_______________________________________________
General mailing list
[email protected]
http://developer.marklogic.com/mailman/listinfo/general