write_json

Write JSON file

File write_json(X)

Writes a JSON file with the serialized form of a WDL value. The following WDL types can be serialized:

WDL TypeJSON Type
Structobject
Objectobject
Map[String, X]object
Array[X]array
Intnumber
Floatnumber
Stringstring
Filestring
Booleanboolean
Nonenull

When serializing compound types, all nested types must be serializable or an error is raised.

Parameters

  1. X: A WDL value of a supported type.

Returns: A File.

Example: write_json_fail.wdl

version 1.2

workflow write_json_fail {
  Pair[Int, Map[Int, String]] x = (1, {2: "hello"})
  # this fails with an error - Map with Int keys is not serializable
  File f = write_json(x)
}

Example input:

{}

Example output:

{}

Test config:

{
  "fail": true
}

Example: write_json_task.wdl

version 1.2

task write_json {
  input {
    Map[String, String] map = {"key1": "value1", "key2": "value2"}
  }

  command <<<
    python <<CODE
    import json
    import sys
    with open("~{write_json(map)}") as js:
      d = json.load(js)
    json.dump(list(d.keys()), sys.stdout)
    CODE
  >>>

  output {
    Array[String] keys = read_json(stdout())
  }
  
  requirements {
    container: "python:latest"
  }
}

Example input:

{}

Example output:

{
  "write_json.keys": ["key1", "key2"]
}

The actual command line might look like:

python <<CODE
import json
with open("local/fs/tmp/map.json") as js:
  d = json.load(js)
  print(list(d.keys()))
CODE

And /local/fs/tmp/map.json would contain:

Each line is terminated by the newline (\n) character.

{
  "key1": "value1",
  "key2": "value2"
}