How to properly build and append a json file from variables - json

I'm practising programming an application that takes user input and then outputs it to a json file.
I found a how to that explains how to do it. For the sake of length, I'm leaving out the input code and just including the json builder.
ASSIGN
uComp = "testCompany"
uEmail = "testEmail"
uName = "testName"
uAdd = "Additional"
.
DEFINE VARIABLE myObj AS JsonObject NO-UNDO.
DEFINE VARIABLE myData AS JsonObject NO-UNDO.
DEFINE VARIABLE dataParams AS JsonObject NO-UNDO.
DEFINE VARIABLE lResult AS LONGCHAR NO-UNDO
VIEW-AS EDITOR LARGE SIZE 60 BY 16.
DEFINE VARIABLE lJArray AS JsonArray NO-UNDO.
DEFINE VARIABLE lAnotherArray AS JsonArray NO-UNDO.
OUTPUT TO "output path.json".
myObj = NEW JsonObject().
dataParams = NEW JsonObject().
myObj:Add("id", "01").
dataParams:Add("Company_name", uComp).
dataParams:Add("uEmail", uEmail).
dataParams:add("uName", uName).
dataParams:add("AddInfo", uAdd).
lJArray = NEW JsonArray().
lJArray:Add(dataParams).
myObj:Add("data", lJArray).
myObj:Write(lResult, TRUE).
DISPLAY lResult.
That part works fine, but my output is like so:
lResult-----------------------------------------------------
{
"id": "01",
"data": [
{
"Company_name": "testCompany",
"uEmail": "testEmail",
"uName": "testName",
"AddInfo": "Additional"
}
]
}
how do I prevent the
lResult-----------
from being added to the file.
Secondly, I want to add additional information to the file when the code runs again so that the output will become.
{
"id": "01",
"data": [
{
"Company_name": "testCompany",
"uEmail": "testEmail",
"uName": "testName",
"AddInfo": "Additional"
},
{
"Company_name": "testCompany",
"uEmail": "testEmail",
"uName": "testName",
"AddInfo": "Additional"
}
]
}
What is the correct way to target a point in the file and add additional objects?
I though it might be something along the lines of an
append
property.

I would leave the complete JSON I/O to the JSON parser in the language. So instead of the append, I'd read in the file into a JSON object and add the additional objects/properties in memory and write back to a file.
Just an output with append won't produce value JSON. This should work:
FILE-INFORMATION:FILE-NAME = "myfile.json" .
IF FILE-INFORMATION:FULL-PATHNAME > "":U THEN DO:
myObj = CAST ((NEW ObjectModelParser()):ParseFile(FILE-INFORMATION:FULL-PATHNAME),
JsonObject) .
lJArray = myObj:GetJsonArray("data") .
END.
ELSE DO:
myObj = NEW JsonObject().
myObj:Add("id", "01").
lJArray = NEW JsonArray().
myObj:Add("data", lJArray).
END.
dataParams = NEW JsonObject().
dataParams:Add("Company_name", uComp).
dataParams:Add("uEmail", uEmail).
dataParams:add("uName", uName).
dataParams:add("AddInfo", uAdd).
lJArray:Add(dataParams).
myObj:WriteFile("myfile.json", TRUE).

how do I prevent the
lResult-----------
from being added to the file.
I suspect it's because the variable has a VIEW-AS phrase on the definition. But using the JsonObject as an object and calling the WriteFile method is the (far) better approach.

Related

Unable to use JSON input for Terraform a map variable in Azure VM extension setting

I am creating an SQL extension for a Azure Window VM using variable type map.
My module is defined as below :
resource "azurerm_virtual_machine_extension" "virtual_machine_extension"{
for_each = var.virtual_machine_extensions
name = each.value["name"]
virtual_machine_id = lookup(var.virtual_machine_ids, each.value["virtual_machine_variable_name"], null)
publisher = each.value["publisher"]
type = each.value["type"]
type_handler_version = each.value["type_handler_version"]
settings = jsonencode(each.value["settings"])
tags = each.value["virtual_machine_extension_tags"]
}
and my variable is defined as below :
variable "virtual_machine_extensions" {
type = map(object({
name = string
virtual_machine_variable_name = string
publisher = string
type = string
type_handler_version = string
settings = any
virtual_machine_extension_tags = map(string)
}))
}
variable "virtual_machine_ids" {
type = map(string)
default = {}
}
virtual_machine_ids will have a list of virtual machines and their corresponding ids passed to it from the main file
My tfvars file looks like this :
virtual_machine_extensions = {
virtual_machine_extension_sql ={
name = "vmsqlext" #Required
virtual_machine_variable_name = "windows_virtual_machine1"
publisher = "Microsoft.SqlServer.Management"
type = "SqlIaaSAgent"
type_handler_version = "1.2"
settings =<<SETTINGS
{
"AutoTelemetrySettings": {
"Region": "West Europe"
},
"AutoPatchingSettings": {
"PatchCategory": "WindowsMandatoryUpdates",
"Enable": true,
"DayOfWeek": "Sunday",
"MaintenanceWindowStartingHour": "2",
"MaintenanceWindowDuration": "60"
},
"KeyVaultCredentialSettings": {
"Enable": false,
"CredentialName": ""
},
"ServerConfigurationsManagementSettings": {
"SQLConnectivityUpdateSettings": {
"ConnectivityType": "Public",
"Port": "1433"
},
"SQLWorkloadTypeUpdateSettings": {
"SQLWorkloadType": "GENERAL"
},
"AdditionalFeaturesServerConfigurations": {
"IsRServicesEnabled": "true"
},
"protectedSettings": {}
}
}
SETTINGS
virtual_machine_extension_tags = {
applicationName = "Windows VM Extension"
approver = "IT Infrastructure"
}
}
}
I am receiving the below error when I run apply:
Error: unable to parse settings: json: cannot unmarshal string into Go value of type map[string]interface {}
A screenshot of the same:
understand that the issue is coming because the JSON input is not getting read properly , I tried running the code by using <<EOF EOF instead of <<SETTING SETTING as well but it's still throwing the same error
You have assigned to var.virtual_machine_extensions.settings a string that already contains JSON, and then in your main configuration you've passed that string to Terraform's jsonencode function which means that you are assigning a string containing a JSON serialization of a string containing JSON.
It's unfortunate that the provider just passes through the underlying Go JSON parser's error message here, making it unclear exactly what's going on by using Go type system terminology instead of JSON terminology, but what this really means is that the provider is expecting a JSON object, but you've assigned a JSON string instead.
The solution is to remove the jsonencode call and just assign your string directly to settings, because that string already contains a valid JSON object and so does not need any further encoding:
settings = each.value.settings

How to get a key value in a json object and affect it into the same object?

I have a json object with some keys and values.
I would like to get a generic way to get one of these values and set (into the same object) a variable with this value.
myObj = {
"name":"John",
"age":30,
"cars": {
"key":"John",
"car2":"BMW",
"car3":"Fiat"
}
}
myObj = {
"name":"John",
"age":30,
"cars": {
"other-name":"John",
"car2":"BMW",
"car3":"Fiat"
}
}
You can use either object.property.anotherProperty form, or object["property"]["anotherProperty"] access form to read and to set value, for example:
myObj.cars.car2 = myObj.cars.car3;
P.S. assuming Javascript
You can assign new value as follow.
myObj.cars.newKey = "Any Value"
That's some nice answer but I would like to know when im writting inside the jsonfile on vi

Apache Nifi manipulate nested json with Execute Script

I am looking for a way to update the given Json's values and keys in a dynamic way. The way the Json is delivered is Always the same(in Terms of structure). The only Thing that differs is the amount of Data that is provided. So for example there could sometimes be 30, sometimes only 10 nestings etc.
…
"ampdata": [
{
"nr": "303",
"code": "JGJGh4958GH",
"status": "AVAILABLE",
"ability": [ "" ],
"type": "wheeled",
"conns": [
{
"nr": "447",
"status": "",
"version": "3",
"format": "sckt",
"amp": "32",
"vol": "400",
"vpower": 22
}
]
}
As Json uses other keys/values than I in my DB, I Need to convert them. Additionally I Need to Change some values if they match explicit strings.
So for example: "Code" has to be renamed to"adrID" and "sckt" should map to the values "bike".
I tried a simple Groovy-Script to remove the key and or Change the value. There is no Problem in changing values, but in changing the key itself. So I tried removing the key and adding a new key. Unfortunately I could not figure out how to add a new key:value to the given json. So how can I add a new pair of key:value or rename the key, if that´s possible. Have a look at my code-example
def flowFile = session.get()
if (!flowFile) return
try {
flowFile = session.write(flowFile,
{ inputStream, outputStream ->
def text = IOUtils.toString(inputStream, StandardCharsets.UTF_8)
def obj = new JsonSlurper().parseText(text)
def objBuilder = new JsonBuilder(obj)
// Update ingestionDate field with today's date
for(i in 0..obj.data.size()-1){
obj.data[0].remove("postal_code")
objBuilder.data[0].postal_code=5
}
// Output updated JSON
def json = JsonOutput.toJson(obj)
outputStream.write(JsonOutput.prettyPrint(json).getBytes(StandardCharsets.UTF_8))
} as StreamCallback)
flowFile = session.putAttribute(flowFile, "filename", flowFile.getAttribute('filename').tokenize('.')[0]+'_translated.json')
session.transfer(flowFile, REL_SUCCESS)
} catch(Exception e) {
log.error('Error during JSON operations', e)
session.transfer(flowFile, REL_FAILURE)
}
...
def obj = new JsonSlurper().parse(inputStream, "UTF-8")
obj.data.each{e->
def value = e.remove("postal_code")
//set old value with a new key into object
e["postalCode"] = value
}
//write to output
def builder = new JsonBuilder(obj)
outputStream.withWriter("UTF-8"){ it << builder.toPrettyString() }

Dynamically build json using groovy

I am trying to dynamically build some json based on data I retrieve from a database. Up until the opening '[' is the "root" I guess you could say. The next parts with name and value are dynamic and will be based on the number of results I get from the db. I query the db and then the idea was to iterate through the result adding to the json. Can I use jsonBuilder for the root section and then loop with jsonSlurper to add each additional section? Most of the examples I have seen deal with a root and then a one time "slurp" and then joining the two so wasn't sure if I should try a different method for looping and appending multiple sections.
Any tips would be greatly appreciated. Thanks.
{
"hostname": "$hostname",
"path": "$path",
"extPath": "$extPath",
"appName": "$appName",
"update": {"parameter": [
{
"name": "$name",
"value": "$value"
},
{
"name": "$name",
"value": "$value"
}
]}
}
EDIT: So what I ended up doing was just using StringBuilder to create the initial block and then append the subsequent sections. Maybe not the most graceful way to do it, but it works!
//Create the json string
StringBuilder json = new StringBuilder("""{
"hostname": "$hostname",
"path": "$path",
"extPath": "$extPath",
"appName": "$appName",
"update": {"parameter": ["""
)
//Append
sql.eachRow("""<query>""",
{ params ->
json.append("""{ "name": "params.name", "value": "params.value" },""");
}
)
//Add closing json tags
json.append("""]}}""")
If I got your explanation correctly and if the data is not very big (it can live in memory), I'd build a Map object (which is very easy to work with in groovy) and convert it to JSON afterwards. Something like this:
def data = [
hostname: hostname,
path: path,
extPath: extPath,
appName: appName,
update: [parameter: []]
]
sql.eachRow(sqlStr) { row ->
data.update.parameter << [name: row.name, value: row.value]
}
println JsonOutput.toJson(data)
If you're using Grails and Groovy you can utilize grails.converters.JSON.
First, define a JSON named config:
JSON.createNamedConfig('person') {
it.registerObjectMarshaller(Person) {
Person person ->
def output = [:]
output['name'] = person.name
output['address'] = person.address
output['age'] = person.age
output
}
}
This will result in a statically defined named configuration for the Object type of person. Now, you can simply call:
JSON.use('person') {
Person.findAll() as JSON
}
This will return every person in the database with their name, address and age all in one JSON request. I don't know if you're using grails as well in this situation though, for pure Groovy go with another answer here.

JSON String formed improperly using Jayrock in .NET

I am trying to return a JSON object from an aspx page using Jayrock.JSON.
My code for writing it out is this:
using (JsonTextWriter writer = new JsonTextWriter(Response.Output))
{
writer.WriteStartObject();
for (int i = 0; i < rdr.FieldCount; i++)
{
writer.WriteMember(rdr.GetName(i).ToString());
writer.WriteString(rdr[i].ToString());
}
writer.WriteEndObject();
}
This is inside of an rdr.Read() loop.
The outputted JSON looks like this: (though I added the line breaks manually)
{
"BugID":"1087",
"AddedBy":"",
"BugDate":"5/2/2010 9:45:34 AM",
"BugTitle":"/admin/ajax_thirdpartylog.asp",
"Classify":""
,"ErrPage":"/admin/ajax_thirdpartylog.asp",
"StoreID":"71",
"UserID":"15438",
"ErrDesc":"Type mismatch: 'formatnumber'",
"ErrDump":"*** VARIABLES DUMP ***\r\n\r\n*** Form Variables ***\r\n\r\ncalmonth : 8\r\ncalmonth2 : 8\r\nfromdate : 8/1/2009\r\ncalyear : 2009\r\ntodate : 8/31/2009\r\ncalyear2 : 2009\r\nr : 978402\r\nthirdtype : 1\r\nButton : Generate Third Party Log\r\n\r\n*** Query String Variables ***\r\n\r\n\r\n\r\n*** REPORT END ***\r\n",
"ErrLine":"74",
"DateFixed":"",
"Counter":"16",
"AssignTo":""
}
{
"BugID":"1086",
"AddedBy":"",
"BugDate":"5/1/2010 11:58:54 PM",
"BugTitle":"/admin/Charts/monthsales_s.asp",
"Classify":"",
"ErrPage":"/admin/Charts/monthsales_s.asp",
"StoreID":"402",
"UserID":"141928",
"ErrDesc":"Script timed out",
"ErrDump":"*** VARIABLES DUMP ***\r\n\r\n*** Form Variables ***\r\n\r\n\r\n*** Query String Variables ***\r\n\r\nmonth1 : 9/1/2009\r\nr : 75333803\r\n\r\n\r\n*** REPORT END ***\r\n",
"ErrLine":"0",
"DateFixed":"",
"Counter":"",
"AssignTo":""
}
I'm not really sure what I'm doing wrong, but on my page reading this JSON, when I try to do .evalJSON (using prototypejs) I get errors saying that the JSON is malformed.
Can anyone advise me what to change?
This:
"AssignTo":""
}
{
is the invalid JSON. You can a string to see if it's valid JSON at JSON Lint. I'm not sure what this should be like, but an empty object would be like this (don't need "", brackets reversed, missing comma):
"AssignTo":
{
},
The problem is that you are writing multiple JSON objects whereas what you are probably trying to do is produce a JSON array of JSON objects. Given your code snippet, I'm assuming rdr holds some IDataReader implementation like SqlDataReader. If that's true then you need to modify your code to start and end a JSON array around the outer read loop, as follows:
using (JsonTextWriter writer = new JsonTextWriter(Response.Output))
{
writer.WriteStartArray();
while (rdr.Read())
{
writer.WriteStartObject();
for (int i = 0; i < rdr.FieldCount; i++)
{
writer.WriteMember(rdr.GetName(i).ToString());
writer.WriteString(rdr[i].ToString());
}
writer.WriteEndObject();
}
writer.WriteEndArray();
}
Jayrock will automatically delimit each JSON object value with a comma (,) when inside a JSON array so now the output should resemble the following and valid JSON:
[
{
"BugID":"1087",
"AddedBy":"",
"BugDate":"5/2/2010 9:45:34 AM",
"BugTitle":"/admin/ajax_thirdpartylog.asp",
"Classify":""
,"ErrPage":"/admin/ajax_thirdpartylog.asp",
"StoreID":"71",
"UserID":"15438",
"ErrDesc":"Type mismatch: 'formatnumber'",
"ErrDump":"*** VARIABLES DUMP ***\r\n\r\n*** Form Variables ***\r\n\r\ncalmonth : 8\r\ncalmonth2 : 8\r\nfromdate : 8/1/2009\r\ncalyear : 2009\r\ntodate : 8/31/2009\r\ncalyear2 : 2009\r\nr : 978402\r\nthirdtype : 1\r\nButton : Generate Third Party Log\r\n\r\n*** Query String Variables ***\r\n\r\n\r\n\r\n*** REPORT END ***\r\n",
"ErrLine":"74",
"DateFixed":"",
"Counter":"16",
"AssignTo":""
},
{
"BugID":"1086",
"AddedBy":"",
"BugDate":"5/1/2010 11:58:54 PM",
"BugTitle":"/admin/Charts/monthsales_s.asp",
"Classify":"",
"ErrPage":"/admin/Charts/monthsales_s.asp",
"StoreID":"402",
"UserID":"141928",
"ErrDesc":"Script timed out",
"ErrDump":"*** VARIABLES DUMP ***\r\n\r\n*** Form Variables ***\r\n\r\n\r\n*** Query String Variables ***\r\n\r\nmonth1 : 9/1/2009\r\nr : 75333803\r\n\r\n\r\n*** REPORT END ***\r\n",
"ErrLine":"0",
"DateFixed":"",
"Counter":"",
"AssignTo":""
}
]