Skip to content
Advertisement

How to collect a specific value from a text that is always a few lines below another text value?

To retrieve the values from the JSON file, I use this code:

JavaScript

This is the JSON template:

JavaScript

This is the output text:

JavaScript

I’m trying to collect the value to Net Profit: that is inserted inside Total, the texts can change and the positions too, but whenever there is a Total, there will be a Net Profit:.

The value I want to collect in this case is:

JavaScript

How can I go about getting that specific part of the text value?

Advertisement

Answer

For more complex cases, you may want to look into Regular Expressions (Regex), but for this simple case:

JavaScript

Here, we use the str.find() function which returns the index of a certain part of a string together with string slicing string[start:end] to find only the part of the string needed. The n is a special character which denotes the end of a line.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement