0

Hello not overly familiar with JavaScript quite new to it actually so i pulled bits from other projects and now struggling to wrap things up.

This is for a small RGB lighting project. The json payload message is transported via MQTT and the initial input is a string value as follows "rrr,ggg,bbb".

what i have come up with so far is as follows

(function(i) {

  var rgb = i.split(",");
  var color = {"r":rgb[0],"g":rgb[1],"b":rgb[2]}
  return JSON.stringify({color});

})(input)

Desired result needs to be {"color":{"r":232,"g":200,"b":55}}

the JS gets stored as a .js file and referenced when transformation is needed

any help would be much appreciated.

2
  • 1
    Are you getting an error? What is not working? What kind of help are you looking for? Commented Feb 18, 2020 at 2:00
  • Executing the JS-transformation failed: An error occurred while loading JavaScript. <eval>:5:30 Expected : but found } return JSON.stringify({color}); ^ in <eval> at line number 5 at column number 30 Commented Feb 18, 2020 at 2:05

1 Answer 1

1

It appears you are creating your inner object correctly, but the value you are stringifying is just the inner object wrapped in braces, you need to change your stringify line to:

 return JSON.stringify({"color": color});

The way you currently have it written, would come out as

{{"r":232,"g":200,"b":55}}

Sign up to request clarification or add additional context in comments.

1 Comment

nailed it that was exactly what i was getting. Made the suggested adjustments, got it working. Thank you for the help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.