Search code examples
jsonnet

How to access a response value titled "self"?


I am using jsonnet to process a response that is structured like so:

{
    "resources": [
        {
           "response_body": {
                "id": "test_id",
                "key": "test_key",
                "self": "https://someurl.com/test/1234"
            }
      ........

I am attempting to process hte response with the following jsonnet:

local result = std.parseJson(std.extVar('result'));
local resource = result.resources[0];
{
  result_fields: {
    issue_url: resource.response_body.self,
    issue: resource.response_body.id
  }
}

But it's throwing the error:

JsonnetTokenType.IDENTIFIER expected, got 'self'

How would I go about access the self value from my response?


Solution

  • self is a reserved word (it's a reference to the current object in scope), if you'd still needed to use 'self' (as it's e.g. enforced by the JSON structure in result), you could then do:

    local result = std.parseJson(std.extVar('result'));
    local resource = result.resources[0];
    {
      result_fields: {
        issue_url: resource.response_body['self'],
        issue: resource.response_body.id
      }
    }
    

    Nevertheless, if possible to use a different key/field name there I'd strongly suggest finding another name.