mardi 13 septembre 2016

Parameterized JSON key naming

I have the following JSON:

{
  my_json: {
    id: 1,
    name: "John"
  }
}

How can I customize key name via parameterized like:

def jsonize(custom_key="id")
{
    my_json: {
      "#{custome_key}": 1,
      name: "John"
    }
  }
end

To be output with:

Scenario 1:

=> jsonize

OUTPUT:

{
  my_json: {
    id: 1,
    name: "John"
  }
}

Scenario 2:

=> jsonize("value")

OUTPUT:

{
  my_json: {
    value: 1,
    name: "John"
  }
}

Aucun commentaire:

Enregistrer un commentaire