JSON

From Rosetta Code
Task
JSON
You are encouraged to solve this task according to the task description, using any language you may know.

Load a JSON string into a data structure. Also create a new data structure and serialize it into JSON. Use objects and arrays, and make sure your JSON is valid (http://www.jsonlint.com/).

Go

<lang go>package main import "json" import "fmt"

func main() {

   var data map[string]interface{}
   json.Unmarshal([]byte("{ \"foo\": 1, \"bar\": [10, \"apples\"] }"), &data)
   fmt.Println(data)
   sample := map[string]interface{}{ "blue": []interface{}{1,2}, "ocean": "water" }
   json_string, err := json.Marshal(sample)
   if err == nil {
       fmt.Println(string(json_string))
   } else {
       fmt.Println(err)
   }

}</lang>

JavaScript

Requires JSON library, now present in some browsers. <lang JavaScript>var data = JSON.parse('{ "foo": 1, "bar": [10, "apples"] }');

var sample = { "blue": [1,2], "ocean": "water" }; var json_string = JSON.stringify(sample);</lang>

Of course, it IS called JavaScript Object Notation, so it is a JavaScript object literal, and you can, alternately, parse it by just eval'ing it. This should work in any browser without a library. (Eval may be dangerous, depending on the source of the data.) However, there is an ambiguity with parsing JavaScript object literals by themselves, where it might be mistakenly interpreted as a block, and the key followed by a colon as a label. To avoid this, remember to surround it in parentheses to force it to be interpreted as an expression: <lang javascript>var data = eval('(' + '{ "foo": 1, "bar": [10, "apples"] }' + ')');</lang>

Oz

With the JSON library from here: <lang oz>declare

 [JSON] = {Module.link ['JSON.ozf']}
 {System.show {JSON.decode "{ \"foo\": 1, \"bar\": [10, \"apples\"] }"}}
 Sample = object(blue:array(1 2) ocean:"water")
 {System.showInfo {JSON.encode Sample}}</lang>

Output:

object(bar:array(10 [97 112 112 108 101 115]) foo:1)
{"blue":[1,2],"ocean":"water"}

PHP

<lang php><?php $data = json_decode('{ "foo": 1, "bar": [10, "apples"] }'); // dictionaries will be returned as objects $data2 = json_decode('{ "foo": 1, "bar": [10, "apples"] }', true); // dictionaries will be returned as arrays

$sample = array( "blue" => array(1,2), "ocean" => "water" ); $json_string = json_encode($sample); ?></lang>

PicoLisp

PicoLisp has no JSON library, but it is easy enough to write one: <lang PicoLisp>(de checkJson (X Item)

  (unless (= X Item)
     (quit "Bad JSON" Item) ) )

(de readJson ()

  (case (read "_")
     ("{"
        (make
           (for (X (readJson)  T  (readJson))
              (checkJson ":" (readJson))
              (link (cons X (readJson)))
              (T (= "}" (setq X (readJson))))
              (checkJson "," X) ) ) )
     ("["
        (make
           (link T)  # Array marker
           (for (X (readJson)  T  (readJson))
              (link X)
              (T (= "]" (setq X (readJson))))
              (checkJson "," X) ) ) )
     (T @) ) )

(de printJson (Item) # For simplicity, without indentation

  (cond
     ((atom Item) (print Item))
     ((=T (car Item))
        (prin "[")
        (map
           '((X)
              (printJson (car X))
              (and (cdr X) (prin ", ")) )
           (cdr Item) )
        (prin "]") )
     (T
        (prin "{")
        (map
           '((X)
              (print (caar X))
              (prin ": ")
              (printJson (cdar X))
              (and (cdr X) (prin ", ")) )
           Item )
        (prin "}") ) ) )</lang>

This reads/prints JSON from/to files, pipes, sockets etc. To read from a string, a pipe can be used:

: (pipe (prinl "{ \"foo\": 1, \"bar\": [10, \"apples\"] }")
   (readJson) )
-> (("foo" . 1) ("bar" T 10 "apples"))

: (printJson
   (quote
      ("name" . "Smith")
      ("age" . 25)
      ("address"
         ("street" . "21 2nd Street")
         ("city" . "New York")
         ("state" . "NY")
         ("zip" . "10021") )
      ("phone" T "212 555-1234" "646 555-4567") ) )
{"name": "Smith", "age": 25, ... {"street": ... "phone": ["212 555-1234", ...

Python

Works with: Python version 2.6+

<lang Python>import json data = json.loads('{ "foo": 1, "bar": [10, "apples"] }')

sample = { "blue": [1,2], "ocean": "water" } json_string = json.dumps(sample)</lang>