JSON: Difference between revisions

13 bytes added ,  4 years ago
Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 5:
Use objects and arrays (as appropriate for your language)
and make sure your JSON is valid (https://jsonformatter.org).
 
 
=={{header|8th}}==
Line 23 ⟶ 22:
</pre>
That takes the object and converts to the JSON string given above.
 
=={{header|Ada}}==
=== Alternative using GNATCOLL ===
Line 270:
"married": false
}</pre>
 
 
=={{header|Bracmat}}==
Line 934 ⟶ 933:
}</lang>
<pre>{"foo":1,"bar":[10,"apples"]}</pre>
 
 
=={{header|Dart}}==
Line 1,173 ⟶ 1,171:
SysLib.writeStdout(result.results[1].geometry.location.lng);
end</lang>
 
 
=={{header|Elena}}==
Line 1,256 ⟶ 1,253:
Erlang -> JSON
{"firstName":"John","lastName":"Smith","age":25,"address":{"streetAddress":"21 2nd Street","city":"New York","state":"NY","postalCode":"10021"},"phoneNumber":[{"type":"home","number":"212 555-1234"},{"type":"fax","number":"646 555-4567"}]}</pre>
 
=={{header|F_Sharp|F#}}==
There are several ways:
 
1. Using Json.Net
<lang fsharp>
open Newtonsoft.Json
type Person = {ID: int; Name:string}
let xs = [{ID = 1; Name = "First"} ; { ID = 2; Name = "Second"}]
 
let json = JsonConvert.SerializeObject(xs)
json |> printfn "%s"
 
let xs1 = JsonConvert.DeserializeObject<Person list>(json)
xs1 |> List.iter(fun x -> printfn "%i %s" x.ID x.Name)
</lang>
 
Print:
<lang fsharp>[{"ID":1,"Name":"First"},{"ID":2,"Name":"Second"}]
1 First
2 Second
</lang>
2. Using FSharp.Data
<lang fsharp>open FSharp.Data
open FSharp.Data.JsonExtensions
 
type Person = {ID: int; Name:string}
let xs = [{ID = 1; Name = "First"} ; { ID = 2; Name = "Second"}]
 
let infos = xs |> List.map(fun x -> JsonValue.Record([| "ID", JsonValue.Number(decimal x.ID); "Name", JsonValue.String(x.Name) |]))
|> Array.ofList |> JsonValue.Array
 
infos |> printfn "%A"
match JsonValue.Parse(infos.ToString()) with
| JsonValue.Array(x) -> x |> Array.map(fun x -> {ID = System.Int32.Parse(string x?ID); Name = (string x?Name)})
| _ -> failwith "fail json"
|> Array.iter(fun x -> printfn "%i %s" x.ID x.Name)</lang>
Print:
<lang fsharp>[
{
"ID": 1,
"Name": "First"
},
{
"ID": 2,
"Name": "Second"
}
]
1 "First"
2 "Second"
</lang>
3. Alternative way of parsing: JsonProvider
<lang fsharp>open FSharp.Data
type Person = {ID: int; Name:string}
type People = JsonProvider<""" [{"ID":1,"Name":"First"},{"ID":2,"Name":"Second"}] """>
 
People.GetSamples()
|> Array.map(fun x -> {ID = x.Id; Name = x.Name} )
|> Array.iter(fun x -> printfn "%i %s" x.ID x.Name) </lang>
Print:<lang fsharp>
1 First
2 Second
</lang>
 
=={{header|Factor}}==
Line 1,397 ⟶ 1,457:
}
</pre>
 
=={{header|F_Sharp|F#}}==
There are several ways:
 
1. Using Json.Net
<lang fsharp>
open Newtonsoft.Json
type Person = {ID: int; Name:string}
let xs = [{ID = 1; Name = "First"} ; { ID = 2; Name = "Second"}]
 
let json = JsonConvert.SerializeObject(xs)
json |> printfn "%s"
 
let xs1 = JsonConvert.DeserializeObject<Person list>(json)
xs1 |> List.iter(fun x -> printfn "%i %s" x.ID x.Name)
</lang>
 
Print:
<lang fsharp>[{"ID":1,"Name":"First"},{"ID":2,"Name":"Second"}]
1 First
2 Second
</lang>
2. Using FSharp.Data
<lang fsharp>open FSharp.Data
open FSharp.Data.JsonExtensions
 
type Person = {ID: int; Name:string}
let xs = [{ID = 1; Name = "First"} ; { ID = 2; Name = "Second"}]
 
let infos = xs |> List.map(fun x -> JsonValue.Record([| "ID", JsonValue.Number(decimal x.ID); "Name", JsonValue.String(x.Name) |]))
|> Array.ofList |> JsonValue.Array
 
infos |> printfn "%A"
match JsonValue.Parse(infos.ToString()) with
| JsonValue.Array(x) -> x |> Array.map(fun x -> {ID = System.Int32.Parse(string x?ID); Name = (string x?Name)})
| _ -> failwith "fail json"
|> Array.iter(fun x -> printfn "%i %s" x.ID x.Name)</lang>
Print:
<lang fsharp>[
{
"ID": 1,
"Name": "First"
},
{
"ID": 2,
"Name": "Second"
}
]
1 "First"
2 "Second"
</lang>
3. Alternative way of parsing: JsonProvider
<lang fsharp>open FSharp.Data
type Person = {ID: int; Name:string}
type People = JsonProvider<""" [{"ID":1,"Name":"First"},{"ID":2,"Name":"Second"}] """>
 
People.GetSamples()
|> Array.map(fun x -> {ID = x.Id; Name = x.Name} )
|> Array.iter(fun x -> printfn "%i %s" x.ID x.Name) </lang>
Print:<lang fsharp>
1 First
2 Second
</lang>
 
=={{header|Go}}==
Line 3,040 ⟶ 3,037:
my $sample = { blue => [1,2], ocean => "water" };
my $json_string = encode_json($sample);</lang>
 
=={{header|Perl 6}}==
 
Using [http://github.com/moritz/json/ JSON::Tiny]
 
<lang perl6>use JSON::Tiny;
 
my $data = from-json('{ "foo": 1, "bar": [10, "apples"] }');
 
my $sample = { blue => [1,2], ocean => "water" };
my $json_string = to-json($sample);</lang>
 
=={{header|Phix}}==
Line 3,383 ⟶ 3,369:
(write-json '(1 2 "three" #hash((x . 1) (y . 2) (z . 3))))
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
Using [http://github.com/moritz/json/ JSON::Tiny]
 
<lang perl6>use JSON::Tiny;
 
my $data = from-json('{ "foo": 1, "bar": [10, "apples"] }');
 
my $sample = { blue => [1,2], ocean => "water" };
my $json_string = to-json($sample);</lang>
 
=={{header|REBOL}}==
Line 3,515 ⟶ 3,513:
; {"foo": "bar", "baz": [1, 2, 3], "qux": {"foo": "bar"}}
</lang>
 
 
=={{header|SenseTalk}}==
Line 3,547 ⟶ 3,544:
a Dictionary('bar'->#(10 'apples') 'foo'->1 )
</pre>
 
=={{header|Standard ML}}==
Works on Unix/Linux/BSD with jq (github.com/stedolan/jq/ ) installed. Data storage in strings, so floating point numbers can be written back as received, in a recursive polymorphic structure, which can also be used to store the data as SML-types. (Without Jq on the system or on Microsoft systems, delete the Validate function and its call, and the code can be used for valid JSON-strings without any white space outside strings (only).)
10,343

edits