Storing JSON data in an item

Hi,

I know how to read/interpret JSON data from an item. I was now trying the opposite, saving JSON data in an item (so I could interface it to another system).

I would like to store this information in an item:

{
	"Status": "Armed",
	"User": "John"
}

I tried using a rule. I figured escape symbols were needed for the quotes. So I did this:

postUpdate(Tex_Log_Alarm_Status_V3, "{ \"status\":\"Armed\", \"User\":\"John\" }

But I got:

Configuration model 'test.rules' has errors, therefore ignoring it: [30,39]: missing ')' at '{'
[30,41]: mismatched input '\' expecting '}'

The cause is that I didn’t escape the {} symbols. But when I try this:

postUpdate(Tex_Log_Alarm_Status_V3, "\{ \"status\":\"Armed\", \"User\":\"John\" \}

I get:

Configuration model 'test.rules' has errors, therefore ignoring it: [28,39]: Invalid escape sequence (valid ones are  \b  \t  \n  \f  \r  \"  \'  \\ )
[30,39]: no viable alternative at input '\'

So I’m kinda stuck here. Does anyone know how to get JSON data in a string item?

You are missing the closing quote:

\"John\" }")
2 Likes

What @rlkoshak says.

I have tested it recently.

postUpdate(jsonTest, "{ \"status\":\"Armed\", \"User\":\"John\" }")

Works fine for me

20:20:30.218 [INFO ] [marthome.event.ItemStateChangedEvent] - jsonTest changed from NULL to { "status":"Armed", "User":"John" }

Damn, I was so focused on the {} and the “”, I forgot to look at the obvious stuff.

Thanks!