Reading data

Unpacking files #

Origami natively understands a number of common file types used by web developers, such as JSON and YAML files. These files are generally recognized by their file extension (.json, .yaml, etc.).

If you give Origami a path inside a file, it will implicitly use the file’s extension to identify how it should unpack that file’s data. It will then extract the requested data from that file.

For example, teamData.yaml contains some data about people. If you ask for the file, you get the whole file back:

$ ori teamData.yaml
- name: Alice
  image: van.jpg
  location: Honolulu
  bio: After working as a manager for numerous startups over the years, I
    decided to take the plunge and start a business of my own.
- name: Bob
  image: kingfisher.jpg
  location: Los Angeles
  bio: Having been an art student for 11 years, I constantly explore various
    disciplines to incorporate artistic pursuits into product design studies.
- name: Carol
  image: venice.jpg
  location: Venice
  bio: I open the line of communication between clients, customers, and
    businesses to get projects done.

But you can provide a slash-separated path to get data out of the file:

$ ori teamData.yaml/0/name
Alice

Alternately, you can use array and property synax:

$ ori "(teamData.yaml)[1].location"
Los Angeles

You can define your own file extension handlers for custom file types.

Passing data to functions #

You can use Origami’s ability to read data out of files to pass data directly to a function written in JavaScript, Origami, or WebAssembly.

$ ori greet.js teamData.yaml/2/name
Hello, Carol. 

Passing data as arguments #

You can combine the above ability with the Origami / JavaScript spread syntax to define an array of function arguments in a file.

If you want to repeatedly test or invoke a JavaScript function under particular conditions – say, to test a network service – you can put those in a JSON, YAML, or Origami file. You can then use the spread operator to pass that array as the arguments to a function.

For example, you could call the standard JavaScript fetch method with arguments that make a call to a web service that echoes the request back to you:

$ ori postArgs.yaml
- https://postman-echo.com/post
- method: POST
  body: This is the body of the POST request.
$ ori fetch ...postArgs.yaml
{"args":{},"data":"This is the body of the POST request.","files":{},"form":{},"headers":{"host":"postman-echo.com","content-length":"37","accept-encoding":"gzip, br","accept-language":"*","x-forwarded-proto":"https","accept":"*/*","content-type":"text/plain;charset=UTF-8","user-agent":"node","sec-fetch-mode":"cors"},"json":null,"url":"https://postman-echo.com/post"}

If you want to pass files as arguments, you can reference those in an Origami file. You could send a local hello.md file to the web echo service:

$ ori hello.md
Hello, world.
$ ori putArgs.ori
[
  "https://postman-echo.com/put"
  {
    method: "PUT"
    body: hello.md
  }
]
$ ori fetch ...putArgs.ori
{"args":{},"data":"Hello, world.","files":{},"form":{},"headers":{"host":"postman-echo.com","content-length":"13","accept-encoding":"gzip, br","accept-language":"*","x-forwarded-proto":"https","accept":"*/*","sec-fetch-mode":"cors","user-agent":"node","content-type":"application/json"},"json":null,"url":"https://postman-echo.com/put"}

Here a set of arguments was passed to fetch. The body argument referenced a local file hello.md which was then uploaded to the service.

Unpacking web resources #

Origami can also implicitly unpack data retrieved from network locations.

The network location https://weborigami.org/samples/help/catBreeds.csv provides a small data set about cats. You can set that URL in parentheses, then traverse the resulting download data with a slash path or array/property syntax to extract specific data within that resource:

$ ori "(https://weborigami.org/samples/help/catBreeds.csv)[1]"
breed: Persian
origin: Iran
size: medium
coat: long
temperament: Calm and Affectionate

 

Next: Using trees with the ori CLI ยป