1

I have an Xpath property inside of a JSON file and I'd like to get two substrings from this Xpath to assisting these substrings into two variables.

The JSON object is as follows;

    {
        'selectGateway':'0',
        'waitingTime':'20000',
        'status':'200',
        'correlationID':'1',
        'matchString':[{'xpath':'/whitelist/locations/location/var-fields/var-field[@key="whitelist-entry" and @value="8.0440147AA44A80"]','value':''}],
        'matchInteger':[],
        'matchSortedList':[]

    }

This is my attempt so far it's working properly, I'm just looking for a way to do this more dynamically and in a better way if it's possible.

int firstStringPositionForKey = matchString[index].xpath.IndexOf("@key=\"");
int secondStringPositionForKey = matchString[index].xpath.IndexOf("\" and");
string betweenStringForKey = matchString[index].xpath.Substring(firstStringPositionForKey+6, secondStringPositionForKey-firstStringPositionForKey-6);

int firstStringPositionForValue = matchString[index].xpath.IndexOf("@value=\"");
int secondStringPositionForValue = matchString[index].xpath.IndexOf("\"]");
string betweenStringForValue = matchString[index].xpath.Substring(firstStringPositionForValue+8, secondStringPositionForValue-firstStringPositionForValue-8);

I expect the output to be like:

key is : whitelist-entry
value is : 8.0440147AA44A80
2
  • 1
    What you tried so far? show us your attempts. Hint: you can use Split('/').Last() Commented Sep 27, 2019 at 11:49
  • I've added my attempt in main question, you may check it. Commented Sep 27, 2019 at 11:54

2 Answers 2

1

I believe you are getting value of xPath in matchString[index].xpath, so here is the solution

//Test is nothing but your xPath
string test = "/whitelist/locations/location/var-fields/var-field[@key=\"whitelist-entry\" and @value=\"8.0440147AA44A80\"]";

//Split your string by '/' and get last element from it.
string lastElement = test.Split('/').LastOrDefault();

//Use regex to get text present in "<text>"
var matches = new Regex("\".*?\"").Matches(lastElement);

//Remove double quotes         
var key = matches[0].ToString().Trim('\"');
var @value = matches[1].ToString().Trim('\"');;

//Print key and value   
Console.WriteLine("Key is: ${key}");
Console.WriteLine("Value is: ${value}");

Output:

Key is: whitelist-entry
Value is: 8.0440147AA44A80

.net fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot, this is exactly what I'm looking for.
I am glad that my answer helped. Kindly accept answer so it will help others to read
0

Using Regex (Link to formula)

var obj = JObject.Parse("your_json");
var xpath = ((JArray)obj["matchString"])[0]["xpath"].Value<string>();

string pattern = "(?<=key=\")(.*?)(?=\").*(?<=value=\")(.*?)(?=\")";
var match = new Regex(pattern).Match(xpath);

string key = match.Groups[1].Value;   
string value = match.Groups[2].Value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.