Skip to content
/ ja2r Public

JSON-API to ruby object conversion

License

Notifications You must be signed in to change notification settings

mkon/ja2r

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5cfc448 · Feb 19, 2025

History

76 Commits
Feb 19, 2025
Aug 21, 2024
Jan 8, 2024
Aug 1, 2024
Aug 28, 2018
Jul 9, 2018
Jan 8, 2024
Jul 9, 2018
Feb 19, 2025
Feb 19, 2025
Jul 9, 2018
May 8, 2022
Feb 19, 2025

Repository files navigation

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