2

I am using this JSON library for Rebol http://ross-gill.com/page/JSON_and_REBOL and I am struggling to find the correct syntax to create JSON arrays.

I tried this:

>> to-json [ labels: [ [ label: "one" ] [ label: "two" ] ] ]
== {{"labels":{}}}

I was hoping for something like:

== {{"labels": [ {"label": "one"}, {"label": "two"} ]}}
johnk
  • 1,102
  • 7
  • 12
  • Is there a reverse operation to turn JSON to Rebol? If so, what structure does it use to represent the string you're looking to get as output when it's fed in? – HostileFork says dont trust SE Aug 30 '15 at 03:51
  • As of AltJSON v3.3, set-word! pairs such as the above are now recognised as JSON objects—the example above should work as expected. – rgchris Oct 08 '15 at 05:37

2 Answers2

2
>> ?? labels

labels: [make object! [

        label: "one"

    ] make object! [

        label: "two"

    ]]


>> to-json make object! compose/deep [ labels: [ (labels) ]]

== {{"labels":[{"label":"one"},{"label":"two"}]}}
Graham Chiu
  • 4,856
  • 1
  • 23
  • 41
2

As suggested by @hostilefork using the inverse operation shows us how it works.

>> load-json {{"labels": [ {"label": "one"}, {"label": "two"} ]}}                                                                           
== make object! [
    labels: [
        make object! [
            label: "one"
        ]
        make object! [
            label: "two"
        ]
    ]
]

So we need to create an object containing objects. compose/deep is needed to evaluate the nested ( ) so the objects are created.

to-json make object! compose/deep [
    labels: [
        (make object! [ label: "one" ])
        (make object! [ label: "two" ])
    ]
]

In addition to the object! approach there is another option with a simpler syntax:

>> to-json [
    <labels> [
        [ <label> "one" ]
        [ <label> "two" ]
    ]
]
== {{"labels":[{"label":"one"},{"label":"two"}]}}
johnk
  • 1,102
  • 7
  • 12
  • As of AltJSON v3.3 (16-Sep-2015), set-word! pairs as per the example in the question are now recognised as JSON objects—that example should work as expected. – rgchris Oct 08 '15 at 05:41