Project

ja2r

0.01
A long-lived project that still receives updates
Simple JSON-API to ruby object conversion
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

>= 6.1, < 8.0
 Project Readme

JA2R

Gem Version Depfu

JASON-API to Ruby Object

Converts a JSON-API payload into a ruby object which supports navigation over relationships.

Usage

json = <<-JSON
{
   "data":{
      "id":"1001",
      "type":"persons",
      "attributes":{
         "name":"Bart"
      },
      "relationships":{
         "sister":{
            "data":{
               "id":"1002",
               "type":"persons"
            }
         }
      }
   },
   "included":[
      {
         "id":"1002",
         "type":"persons",
         "attributes":{
            "name":"Lisa"
         }
      }
   ]
}
JSON
bart = JA2R.parse(JSON.parse(json))
bart.sister.name # Lisa

Options

You can pass options to JA2R#parse as second argument. Currently the only supported options are:

Name Type Description
safe_traverse boolean When setting this option to true, unknown relationships or attributes will return nil instead of raising NoMethodError

Example:

bart = JA2R.parse(hash, safe_traverse: true)
bart.uncle&.name # nil