Replace function changing square to curly brackets


#1

1) Give a description of the problem
This is a really silly problem

I am loading a weather API (turns out weather underground still works if you look hard enough) which is meant to be returned as json, but it isnt json, so I need to make it into JSON.

I have done a heap of replace commands on it to put quotes in, pull out stuff I dont lead, sort out the hierarchy of brackets.

However I cannot replace the square brackets with curly brackets, as curly brackets are special (and it seems I can’t even escape them)

The string I am working with (ie the output of the replace command in the piston shown) is this (which needs its brackets changed to be curly)

[“epoch”:1607698549, “humidity”:93, “imperial”:[“dewpt”:47, “elev”:64, “heatIndex”:50, “precipRate”:0.00, “precipTotal”:0.13, “pressure”:29.11, “temp”:50, “windChill”:50, “windGust”:0, “windSpeed”:0], “lat”:50.999469, “lon”:-0.947815, “solarRadiation”:46.0, “uv”:0.0, “winddir”:0]

2) What is the expected behaviour?
I thought replace(@localweatherobs,’[’,’{’) would replace all [ with {

3) What is happening/not happening?
Evaluation error: Invalid expression closure termination

**4) Post a Green Snapshot of the piston![image|45x37]

5) Attach logs after turning logging level to Full
(tested using evaluation console)


#2

I only tried the replace function… data is your

[“epoch”:1607698549, “humidity”:93, “imperial”:[“dewpt”:47, “elev”:64, “heatIndex”:50, “precipRate”:0.00, “precipTotal”:0.13, “pressure”:29.11, “temp”:50, “windChill”:50, “windGust”:0, “windSpeed”:0], “lat”:50.999469, “lon”:-0.947815, “solarRadiation”:46.0, “uv”:0.0, “winddir”:0]

replace(data,'\[','\{','\]','\}')

(expression) replace(data,'\[','\{','\]','\}') »»» (string) {“epoch”:1607698549, “humidity”:93, “imperial”:{“dewpt”:47, “elev”:64, “heatIndex”:50, “precipRate”:0.00, “precipTotal”:0.13, “pressure”:29.11, “temp”:50, “windChill”:50, “windGust”:0, “windSpeed”:0}, “lat”:50.999469, “lon”:-0.947815, “solarRadiation”:46.0, “uv”:0.0, “winddir”:0}


#3

Is it definitely not JSON? Only I’ve found that when you output JSON in the webCoRE logs it rewrites curly brackets to square ones so it looks like it is wrong when it is fine.


#4

@orangebucket I suspect the problem is the JSON weather underground presents does not have quoted strings so it fails parsing, so I write it to a variable and fix the quoting. So you may be right in that respect but I’ve broken it by taking the square bracket version and meeting with it. It definitely fails to parse to JSON if taken from the API call.

@eibyer why does that work for you and not me! I’ll give it another go to replicated what you did (subtle as the difference is between yours and mine)


#5

Save the data to a variable first, I think that’s the only difference with our approach.


#6

To clarify what @eibyer said…

Save it to a local string variable first… and then parse that variable.
(you can always dump the final output into a @global, if you want)


#7

Yes that worked. So clearly something odd happens with global variables not local ones. One to remember!


#8
  • Global variables are set after the last line of code has executed…
  • Local variables are set immediately, and can be processed in the very next line.