Groovy string operations and when to give credit

I'm just trying to code my first driver which is also my first time using Groovy. I was hoping someone would be able to help me with extracting the data I need from a string.

I'm making an API call and the string I get back from this call looks like this:
{"Apparent_output":407,"BackupBuffer":"0","BatteryCharging":false,"BatteryDischarging":true,"Consumption_W":430}

How can I for example extract the boolean value of "BatteryCharging" and the integer "Consumption_W" from this string?

That’s JSON..

http://docs.groovy-lang.org/latest/html/gapi/groovy/json/JsonSlurper.html

https://www.tutorialspoint.com/groovy/groovy_json.htm

2 Likes

Oh, that's good (I think). I'm thinking that JSON is easier to extract data from as it is already structured. I'll have a look at this tomorrow, but thank you in advance.

1 Like

Now that I realised that it is JSON (thanks @bcopeland), I searched for parsing JSON on the forum and used what @csteele posted here, Parse JSON response from GET , and expanded on that.

Now, I will take this just slightly off topic and ask about credits in code. As I mentioned at the top of this thread, I am very much a novice at coding and have never really thought about this before:

When would you include credits for having used someone else's code while developing your own driver? For instance, should I give a shout out to @csteele in my code since I used his code from the forum as the basis of parsing the JSON? I have since then run in to many other things that I have searched the forum for and picked bits and pieces from many different threads. I've also used some drivers for inspiration (not taken the whole driver, but a line of code here and a line of code there).

In my travels on the forum I have come across some heated discussions regarding "borrowing" someone else's code and then not acknowledged that in the code. But how much can you "borrow" before it's considered too much without acknowledgements/credits?

Clicking Reply and taking a step back for the potential carnage this question may generate

3 Likes

That's always a judgement call. This forum's TOS states that all user contributions are licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. That means that you are free to share them with or without alteration and with or without the author's express consent as long as you give credit to the original author.

Just how much of the original work has to be left for your work to qualify as an "Adaptation" is one of the great questions of Open Source development, and the answer will vary on a case-by-case basis. In general, I would recommend erring on the side of providing attribution if you have any doubt. In this case, a simple comment in your driver above the function in question would likely be sufficient. i.e.:

// Credit to @csteele on the Hubitat Community Forum for pointing me in the right direction here
// https://community.hubitat.com/t/parse-json-response-from-get/17714/4
def poll() {
	def requestParams = [ uri: myDeviceURL ]
	asynchttpGet("pollHandler", requestParams)
}

def pollHandler(resp, data) {
    //...
}
2 Likes