Build JSON array of strings
Create and output a JSON array of strings
Accept a list of strings as command line arguments. Create and output a JSON array of those strings.
Implementations
Bash
We don’t recommend trying to use Bash, Zsh, or any shell that doesn’t support JSON natively for this task.
C#
JsonArray.cs
using System;
using System.Text.Json;
class JsonArray
{
public static void Main(string[] args)
{
string[] inputArray = args;
string jsonString = JsonSerializer.Serialize(inputArray);
Console.WriteLine(jsonString);
}
}Running the program
$ dotnet run json_array a b c d
["a","b","c","d"]
Go
json_array.go
package main
import (
"encoding/json"
"fmt"
"os"
)
func main() {
args := os.Args[1:]
var jsonArray []string
for _, arg := range args {
jsonArray = append(jsonArray, arg)
}
jsonArrayBytes, _ := json.Marshal(jsonArray)
fmt.Println(string(jsonArrayBytes))
}Running the program
$ go run json_array.go a b c d
["a","b","c","d"]
Java
JsonArray.java
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
public class JsonArray {
public static void main(String[] args) {
var objectMapper = new ObjectMapper();
var arrayNode = objectMapper.createArrayNode();
for (String arg : args) {
arrayNode.add(arg);
}
String jsonArrayString = arrayNode.toString();
System.out.println(jsonArrayString);
}
}Running the program
$ java JsonArray.java a b c d
["a","b","c","d"]
Javascript (Deno)
json_array.mjs
const myStrings = Deno.args
console.log(JSON.stringify(myStrings))Running the program
$ deno run --allow-read --allow-write json_array.mjs a b c d
["a","b","c","d"]
Javascript (Node.js)
json_array.mjs
const myStrings = process.argv.slice(2)
console.log(JSON.stringify(myStrings))Running the program
$ node json_array.mjs a b c d
["a","b","c","d"]
Lua
json_array.lua
local cjson = require("dkjson")
local args = {}
for i = 1, #arg do
table.insert(args, arg[i])
end
print(cjson.encode(args))Running the program
$ lua json_array.lua a b c d
["a","b","c","d"]
PHP
json_array.php
<?php
$myStrings = array_slice($argv, 1);
echo json_encode($myStrings);Running the program
$ php json_array.php a b c d
["a","b","c","d"]
Perl
json_array.pl
use strict;
use warnings;
use JSON;
print encode_json(\@ARGV);Running the program
$ perl json_array.pl a b c d
["a","b","c","d"]
Python
json_array.py
import json
import sys
my_strings = sys.argv[1:]
print(json.dumps(my_strings))Running the program
$ python json_array.py a b c d
["a","b","c","d"]
R
json_array.R
library(jsonlite)
args <- commandArgs(trailingOnly = TRUE)
cat(toJSON(args))Running the program
$ Rscript json_array.R a b c d
["a","b","c","d"]
Raku
json_array.raku
use v6;
use JSON::Fast;
say to-json(@*ARGS);Running the program
$ raku json_array.raku a b c d
["a","b","c","d"]
Ruby
json_array.rb
require 'json'
my_strings = ARGV
puts JSON.generate(my_strings)Running the program
$ ruby json_array.rb a b c d
["a","b","c","d"]
Rust
json_array.rs
//cargo-deps: json="0.12.4"
use json::JsonValue;
use std::env;
extern crate json;
fn main() {
let substrings: Vec<String> = env::args().skip(1).collect();
let json_array: JsonValue = substrings.into();
println!("{}", json_array.dump());
}Running the program
$ cargo script json_array.rs a b c d
["a","b","c","d"]
Swift
json_array.swift
import Foundation
let myStrings = Array(CommandLine.arguments.dropFirst())
let jsonData = try JSONSerialization.data(withJSONObject: myStrings)
print(String(data: jsonData, encoding: .utf8)!)Running the program
$ swift json_array.swift a b c d
["a","b","c","d"]