Home > Enterprise >  Getting the last value of a key value pair in a json like string
Getting the last value of a key value pair in a json like string

Time:06-09

I have a json like string (incomplete json) in which I am trying to retrieve the value of the last key:value pair. The incomplete json like string looks like this:

"BaseCode":null,"BrokerSymbol":null,"CustID":null,"SynthDesc":""}],"@nexturl":"https://someurl.com"}

I am trying to access the value of the @nexturl key and this is the code I've till now:

str1.split(":")[-1]

this gives the output //someurl.com. 2 issues here are that splitting on : removes the https prefix and also doesn't seem like a great approach as if the url contains any more : it will split on that. Is there someway I can get the entire value of @nexturl key?

CodePudding user response:

As requested, assuming your string is as follows:

mystr = '"BaseCode":null,"BrokerSymbol":null,"CustID":null,"SynthDesc":""}],"@nexturl":"https://someurl.com"}'

You can grab the key:value pair containing “@nexturl” by using:

mystr[mystr.index('"@nexturl":'):len(mystr)-1]
  • Related