replace help

Started by joejoe, November 12, 2024, 12:04:58 PM

Previous topic - Next topic

joejoe

Hi and thanks for guidance!

I am trying to format this into valid json so I can json-parse it:

("{'symbol': 'BURT/USDT', 'timestamp': 1731440509530, 'datetime': '2024-11-12T19:41:49.530Z', 'high': 0.00353408, 'low': 0.00208606, 'bid': None, 'bidVolume': None, 'ask': None, 'askVolume': None, 'vwap': None, 'open': 0.00303488, 'close': 0.00246695, 'last': 0.00246695, 'previousClose': None, 'change': -0.00056793, 'percentage': -18.71, 'average': 0.00275091, 'baseVolume': None, 'quoteVolume': 281487.40662491, 'info': {'s': 'burt_usdt', 't': '1731440509530', 'cv': '-0.00056793', 'cr': '-0.1871', 'o': '0.00303488', 'l': '0.00208606', 'h': '0.00353408', 'c': '0.00246695', 'q': '97965253', 'v': '281487.40662491'}, 'indexPrice': None, 'markPrice': None}")
From what I understand, I have to change the single quotes to double quotes and change None to null, in order for it to be valid json, below, which validates as correct json:

{"symbol": "BURT/USDT", "timestamp": 1731440509530, "datetime": "2024-11-12T19:41:49.530Z", "high": 0.00353408, "low": 0.00208606, "bid": null, "bidVolume": null, "ask": null, "askVolume": null, "vwap": null, "open": 0.00303488, "close": 0.00246695, "last": 0.00246695, "previousClose": null, "change": -0.00056793, "percentage": -18.71, "average": 0.00275091, "baseVolume": null, "quoteVolume": 281487.40662491, "info": {"s": "burt_usdt", "t": "1731440509530", "cv": "-0.00056793", "cr": "-0.1871", "o": "0.00303488", "l": "0.00208606", "h": "0.00353408", "c": "0.00246695", "q": "97965253", "v": "281487.40662491"}, "indexPrice": null, "markPrice": null}
Here is what I tried:

(set 'a '("{'symbol': 'BURT/USDT', 'timestamp': 1731439797068, 'datetime': '2024-11-12T19:29:57.068Z', 'high': 0.00353408, 'low': 0.00208606, 'bid': None, 'bidVolume': None, 'ask': None, 'askVolume': None, 'vwap': None, 'open': 0.00294771, 'close': 0.00248834, 'last': 0.00248834, 'previousClose': None, 'change': -0.00045937, 'percentage': -15.58, 'average': 0.00271802, 'baseVolume': None, 'quoteVolume': 281817.75855221, 'info': {'s': 'burt_usdt', 't': '1731439797068', 'cv': '-0.00045937', 'cr': '-0.1558', 'o': '0.00294771', 'l': '0.00208606', 'h': '0.00353408', 'c': '0.00248834', 'q': '97998750', 'v': '281817.75855221'}, 'indexPrice': None, 'markPrice': None}"))

(replace "\'" a "\"")  ; gave the below with no changes

("{'symbol': 'BURT/USDT', 'timestamp': 1731439797068, 'datetime': '2024-11-12T19:29:57.068Z', 'high': 0.00353408, 'low': 0.00208606, 'bid': None, 'bidVolume': None, 'ask': None, 'askVolume': None, 'vwap': None, 'open': 0.00294771, 'close': 0.00248834, 'last': 0.00248834, 'previousClose': None, 'change': -0.00045937, 'percentage': -15.58, 'average': 0.00271802, 'baseVolume': None, 'quoteVolume': 281817.75855221, 'info': {'s': 'burt_usdt', 't': '1731439797068', 'cv': '-0.00045937', 'cr': '-0.1558', 'o': '0.00294771', 'l': '0.00208606', 'h': '0.00353408', 'c': '0.00248834', 'q': '97998750', 'v': '281817.75855221'}, 'indexPrice': None, 'markPrice': None}")

Do I need to use regex for this or what would be the best approach to take? Thanks very much!

rrq

So 'a gets set to a list of a string ? Did you mean to set it to the string rather? like
(set 'a "{'symbol': 'BURT/USDT', ... 'markPrice': None}")

joejoe

Thanks Ralph,

I am using this to get data from a python script:

(set 'a (exec "python3 go.py"))
So when it arrives to my nL script, it is a string inside of a list.

With your suggestion, I managed to get it to the string with this:

(set 'b (a 0))
and can then run this:

(replace "\'" b "\"")
Which returns this:

"{\"symbol\": \"BURT/USDT\", \"timestamp\": None, \"datetime\": None, \"high\": 0.002463, \"low\": 0.001605, \"bid\": None, \"bidVolume\": 1731397.09898224, \"ask\": None, \"askVolume\": 2732456.21823022, \"vwap\": None, \"open\": 0.002136, \"close\": 0.002051, \"last\": 0.002051, \"previousClose\": None, \"change\": -8.5e-05, \"percentage\": -3.9794007490636703, \"average\": 0.002093, \"baseVolume\": 10255192.67905658, \"quoteVolume\": None, \"markPrice\": None, \"indexPrice\": None, \"info\": {\"close\": \"0.002051\", \"high\": \"0.002463\", \"last\": \"0.002051\", \"low\": \"0.001605\", \"market\": \"BURTUSDT\", \"open\": \"0.002136\", \"period\": \"86400\", \"value\": \"21214.1562025586145\", \"volume\": \"10255192.67905658\", \"volume_buy\": \"1731397.09898224\", \"volume_sell\": \"2732456.21823022\"}}"
Is there an way to keep the "\" from appearing, as I was trying to escape the middle " ?

Much appreciated again!

joejoe

Side note, I managed to get a json output from the python file, which feeds nicely into
json-parse.

Of course, I am still curious how I should correctly swap the ' for a " in the
replace function.

rrq

Yes it can be a bit confusing. The string returned by replace does have single-quotes replaced by double-quotes, but the interactive result output presents the double-quotes prefixed by backslash.

Thus, when the string has a double quote, the result printing of that string shows \" so as to indicated that the double-quote is not ending the string. Likewise, the result printing adds the double-quotes before and after printing the string content, and those double-quotes are not characters in the string.

If you make it be
(print (replace ....))then the string will be printed without extras before double-quotes, but the interactive output coming after will still show the string with that meta-character wrapping.

In short, a string of a single double-quote characters is written "\"", and it will be taken as such if input and printed as such by the interactive output. The print function doesn't add meta-characters. Here's an illustration example:
> (println "\"")
"
"\""
>
The first line is what the println function prints (which is the "raw" string content with a newline added at end), and the second line is the interactive output of the value returned by the println function (the same string but with meta-characters to make it be the character sequence the input reader would need).

joejoe

Ok roger that, rrq.

I did not realize the interactive mode would display differently.

And thanks for the println tip, makes it much prettier.

Thanks again for clarification and detailed explanation!

Much appreciated.