2

I have a json file. I want to encode into base64 string and add it in my main json using jsonnet.

datasources.json:

{
  "datasources": [{
    "id": 1,
    "orgId": 1,
    "name": "prometheus"
  }]
}

grafana.jsonnet:

local getDataSources() = {
    'datasources': (import 'datasources.json').datasources,
};

local kp = {
    dashboardDatasources+: {
       data: std.base64(getDataSources().datasources),
    }
}
kp

Please suggest how to get this done. Struggling to convert json into a string in jsonnet.

webwurst
  • 4,830
  • 3
  • 23
  • 32
Alok Kumar Singh
  • 2,331
  • 3
  • 18
  • 37

1 Answers1

3

This can be done using std.manifestJsonEx

std.manifestJsonEx(value, indent) Convert the given object to a JSON form. indent is a string containing one or more whitespaces that are used for indentation:

Here is the solution:

local getDataSources() = {
    'datasources': (import 'datasources.json').datasources,
};

local dataSources = getDataSources().datasources;

local kp = {
    dashboardDatasources+: {
       data: std.base64(std.manifestJsonEx(dataSources, " ")),
    }
}
kp
Alok Kumar Singh
  • 2,331
  • 3
  • 18
  • 37