Json file import and post

Hello Folks,
New to programming, I want to read a big JSON file ( Its actually a configuration), take as it is and POST it to
so that the configuration will manifest. I am also using terraform in my setup. The JSON has more than 100 key values and also has nested key values.
Question is do I have to parse the JSON ? can I take it directly and make a API call to the above URL ? what is the best way of doing ? If I have to parse then I need to create so many structs. Please help

Does the target service expect a json string exactly as the one you read from the file? In that case, you just need to forward it, no need to parse anything.

If instead the service expects something different, then you’ll need to parse and acommodate to the expected format, though depending on it structs could be helpful or maybe not necessary. Check generic json parsing here: https://blog.golang.org/json-and-go.

1 Like

Thanks Iegomez, I just need as is, but terraform takes as string, so I have to parse it I guess ?

If it expects a string, then there’s no parsing needes, just read the json string from file and then use that string to feed the service.

Thanks lgnacio,
Here is the flow I want to achieve

  1. Read the file ( JSON payload) as string
  2. Post it at in Json format only

I can manually curl and POST it at the above server API without any issue. Now question is can we take the Json file abc.Json as it is in GO without converting in string ? and POST it. If not then we need to read as a string which the server wont understand, then probably we have to use struct to fit in the string and then POST ?

Sure. Untested, directly from the keyboard:

bs, err := ioutil.ReadFile("file.json")
if err != nil {
	// appropriate handling
req, err := http.NewRequest(http.MethodPost, "https://example.com/service", bytes.NewReader(bs))
if err != nil {
	// appropriate handling
resp, err := http.DefaultClient.Do(req)
if err != nil {
	// appropriate handling
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
	// it wasn't ok :(

add headers etc as appropriate.

hi Jakob, thanks problem is I am using terraform resource to read the json file, here is the code
iapp := d.Get(“jsonfile”).(string)
iapp gets the file as a string and my struct is as follows
type Iapp struct {
Name string
Partition string
Description string
Devicegroup string
ExecuteAction string
InheritedDevicegroup string
StrictUpdates string
Template string
TemplateModified string
TemplatePrerequisiteErrors string
TrafficGroup string
InheritedTrafficGroup string
Lists []Listsrecord
Metadata []Metadatarecord
Tables []Tablesrecord
Variables []Variablesrecord
Jsonfile string

so I need to populate the struct Iapp with the string which we read and then make a POST to the server

I tried the below code
iapp := d.Get(“jsonfile”).(string)

var iapps Iapp

if err := json.Unmarshal(iapp, &iapps); err != nil {
    log.Fatalf("JSON unmarshaling failed: %s", err)
fmt.Println(iapps) // "[{Casablanca} {Cool Hand Luke} {Bullitt}]"

gave error
go build


bigip/resource_bigip_sys_iapp.go:229:11: undefined: Iapp
bigip/resource_bigip_sys_iapp.go:231:26: cannot use iapp (type string) as type []byte in argument to json.Unmarshal
SJC-ML-SHITOLE:terraform-provider-bigip shitole$
my struct definations are in another file called application.go

I don’t really follow. You say you want to pass it on without interpreting, yet you need to unmarshal it into a struct? Regardless, the error cannot use iapp (type string) as type []byte in argument to json.Unmarshal can be resolved by converting your string to byte slice:

bs := []byte(iapp)

and passing that to Unmarshal. The other error looks like a type of lapp vs iapp.

Thanks I used bs now

jsonfile := []byte(d.Get(“jsonfile”).(string))

 if err := json.Unmarshal(jsonfile, &d); err != nil {
     log.Fatalf("JSON unmarshaling failed: %s", err)

In the above code I read file as string in jsonfile , “d” is my struct but when I unmarshall it gives error below
2018/01/16 12:31:28 JSON unmarshaling failed: invalid character ‘:’ after top-level value

That means your data is not valid JSON.

thanks Jakob you are right, I changed the Json and now it went through fine, however I when I print &d struct it shows “”

jsonfile: “” => "{\n “addressStatus”: “yes”,\n “autoLasthop”: “default”,\n “cmpEnabled”: “yes”,\n “connectionLimit”: 0,\n “destination”:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.