Append to JSON array with ColdFusion, taking Null values into consideration?

与世无争的帅哥 提交于 2020-01-05 04:50:37

问题


I have been digging here on Stack for some time but I do not believe my brain is getting the hint on how to do this.

I need this JSON:

'{"loginHosts":["server1.example.com","server2.example.com"],"sudoHosts":["server1.example.com","server2.example.com"],"CPG":"my_group","mail":"test@example.com","loginShell":"/bin/bash"}'

To look like this JSON:

'{"loginHosts":["server1.example.com","server2.example.com","newserver1.example.com","newserver2.example.com"],"sudoHosts":["server1.example.com","server2.example.com","newserver1.example.com","newserver2.example.com"],"CPG":"my_group","mail":"test@example.com","loginShell":"/bin/bash"}'

There are some caveats.

  1. The JSON application I am pulling from, requires these fields be returned, whether null or not: loginHosts, sudoHosts, CPG, mail, loginShell. If they are not returned the application complains.

  2. The JSON application expects loginHosts, sudoHosts, and CPG to be returned as an array [] even if null. I.E.: '{"loginHosts":[],"sudoHosts":[]}'

  3. The original loginHosts and sudoHosts data (such as: server1.example.com, server2.example.com) must remain in the JSON string sent back to the application. So all of my new data must be appended.

  4. There are many times when loginHosts and sudoHosts are sent to my application as Null arrays. I.E.: '{"loginHosts":[],"sudoHosts":[]}'

I am developing this on a server running Lucee 4.5 with a Linux OS. To get/set the JSON string; I use CFEXECUTE to ssh to a remote Linux server to pull/push my JSON string. You can see the reason for that here: Populating SELECT with large JSON data set via ColdFusion (Lucee) very slow

The code I have so far:

    <cfset user_list = '{"loginHosts":["server1.example.com","server2.example.com"],"sudoHosts":["server1.example.com","server2.example.com"],"CPG":"my_group","mail":"test@example.com","loginShell":"/bin/bash"}'

<cfset arrayOfStructs = deserializeJson(user_list)>

<cfloop array="#arrayOfStructs#" index="user">
    <cfif structKeyExists(user, "loginHosts")>
        <cfloop array="#user.loginHosts#" index="login">
                <cfset loginHosts = #login#>
        </cfloop>
    </cfif>
    <cfif structKeyExists(user, "sudoHosts")>
        <cfloop array="#user.sudoHosts#" index="hosts">
            <cfset sudoHosts = #hosts#>
        </cfloop>
    </cfif>                             
    <cfif structKeyExists(user, "CPG")>
        <cfloop array="#user.CPG#" index="cp">
            <cfset CPG = #cp#>
        </cfloop>
    </cfif>
    <cfset mail = #user.mail#>
    <cfset loginShell = #user.loginShell#>
</cfloop>
<cfset my_servers = "newserver1.example.com,newserver2.example.com">
<cfset loginHosts = listAppend(loginHosts, "#my_servers#", ",")>
<cfset myStruct = '{"loginHosts":["#loginHosts#"],"sudoHosts":["#sudoHosts#"],"CPG":["#cp#"],"mail":"#mail#","loginShell":"#loginShell#"}'/>

The myStruct is then serialized and sent back to the remote server using the CFEXECUTE method I mentioned earlier.

That code works a little but two things are not working. First, each cfset loginHosts and cfset sudoHosts are only returning the second server in the JSON (server2.example.com). I understand that I am overwriting my own loginHosts in that CFLOOP but I am not sure how to correct that and also check if the loginHosts array is Null.

I am also having problems preserving double quotes so that they remain in the JSON. In my testing I get this:

["server1.example.com","server2.example.com","newserver1.example.com,newserver2.example.com"]

Notice the missing "," between newserver1 and newserver2?

I have tried adding escaped quotes to my listAppend() like this: """#my_servers""". It seems to work OK until I have a Null value returned in the JSON string.

Any nudge in the right direction would be appreciated!

EDIT ---

Some clarification:

Why: I am simply wanting to add additional server names to a database. To work with that database I am required to use an API that sends JSON and expects JSON back. This has to be done through a BASH shell script that utilizes curl. As I posted above, this is the reason: Populating SELECT with large JSON data set via ColdFusion (Lucee) very slow

I am using serializeJSON() and deserializeJSON() a lot in my application. The "user_list" at the top of my code is simply there to show you what data I am given to work with. It was not written by hand.

EDIT 2 --

My apologies for not adding this line to my code above.

The 'myStruct' at the bottom gets populated with the data from the "arrayOfStructs" and then serialized.

<cfset myJsonvar = serializeJSON(myStruct)/>

The 'myJsonvar' is then sent as a string, over SSH using CFEXECUTE, to a BASH script that then uses curl to submit the data. Yes, convoluted, but its what I have due to security.


回答1:


I would simplify the code quite a bit. It appears as though all you are doing is adding your two new servers to an existing list of loginHosts within your struct; that doesn't require any looping.

<cfset user_list = '{"loginHosts":["server1.example.com","server2.example.com"],"sudoHosts":["server1.example.com","server2.example.com"],"CPG":"my_group","mail":"test@example.com","loginShell":"/bin/bash"}'>

<cfset myStruct = deserializeJson(user_list)>

<cfset myServers = ["newserver1.example.com","newserver2.example.com"]>

<!--- if loginHosts isn't an array, make it one. --->
<cfif NOT structKeyExists(myStruct, 'loginHosts') OR NOT isArray(myStruct.loginHosts)> 
  <cfset myStruct.loginHosts = []>
</cfif>
<cfset arrayAppend(myStruct.loginHosts, myServers, true)>

<cfset user_list = serializeJSON(myStruct)>
<!--- now user_list is a json string with your new servers added as login hosts, the rest of the struct remains the same. --->


来源:https://stackoverflow.com/questions/37870016/append-to-json-array-with-coldfusion-taking-null-values-into-consideration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!