How to parse a json (encoded in htmlentities) that was saved

Hello,

I’m saving a meta element, but this user session named

contentValue

is encoded in htmlentities.

<meta name="my.Config.1" content="&#x7b;&#x22;resources&#x22;&#x3a......">

Once decoded i know that it’s a json and i want to extract specific value of this json.

http("Get cacheid)
.get("/mypath")
.check(css("meta[name='my.config.1']", "content").saveAs("contentValue"))

if i print it, it is not encoded as htmlentities.

		.exec(session => {
			println(s" This is the Content Value : ${session("contentValue").as[String]}")
			session
		  })

in the console, it will print :

 This is the Content Value : 
{"resources":{"resourceRoots":{
......
.....
:{"targetMappings":{"baseUrl":"/baseurl2,"cacheId":"mycacheid"},"personalization":{"baseUrl":"/url1","relativeUrl":"bar","cacheId":"mycacheid1"},"pbFioriHome":{"baseUrl":"/fullpath/url","cacheId":"mycacheid2"},"startUp":{"baseUrl":"/homepage","relativeUrl":"start_up","cacheId":"mycacheid3"}},"toto2":true"}}

So, how to parse my saved user session’s value contentCache?
i’ve tried with transformwithSession but with no luck.

Thank you for your help.

i’ve tried with transformwithSession

Regular transform is probably enough, I don’t see why you would need to have access to the Session for parsing text into a JSON AST.

but with no luck.

What have you done exactly and how did it not work?

Thank you for your quick response.

i’ve lost the syntax as i made many tests…
i’m trying with simple transform :

.check(jsonPath("$..cacheid").transform(string => string + "DEBUG"))

[ERROR] i.g.c.a.b.SessionHookBuilder$$anon$1 - 'hook-5' crashed with 'j.u.NoSuchElementException: No attribute named 'cacheId' is defined', forwarding to the next one
---- Errors --------------------------------------------------------------------
> jsonPath($..cacheid).find.transform.exists preparation crashed      1 (100,0%)
: Jackson failed to parse into a valid AST: c.f.j.c.JsonParseE...

Using saveAs is a basic notion in Gatling. I really recommend going through the official tutorials and online trainings.

ok, thank you. I suppose i missunderstanding something.
i’m going to check it again.

I’ve rechecked it, however i think i need to understand, if the string retreived with the CSS Selector, can be parsed via json even if it is encoded as “HTML entity”.
so that’s why i first tried to save it, and try to extract the json element i’m looking for and save it…

Maybe, it’s not required to save it first, but i don’t really have better idea.

Thank you.

i think i need to understand, if the string retreived with the CSS Selector, can be parsed via json even if it is encoded as “HTML entity”.

There’s any reason for a CSS selector to automatically unescape it for you. This is application specific.

So in transform, you have to unescape it, and possibly parse it into a JSON AST if that’s hat you’re looking for.

I suppose you wanted me to do something like

.check(css("meta[name='myConfig.1']", "content")
				.transform(css => jsonPath("$..services.targetMappings.cacheId")
				)
				.saveAs("contentValue")

however, when i don’t run the transform, i have the whole json, but if i transform (no matter if i try css => json.jsonPath -that is bringing an error - or something else) when i print the content value i have :

 This is the Content Value : io.gatling.core.check.jsonpath.JsonPathCheckBuilder$$anon$1@7c462bab

definitively, i don’t understand…

i was also looking for differents type, in the cheat sheet (like in the gatling academy course) but there is no more cheat sheet (or i didn’t find it).

I suppose you wanted me to do something like

No. jsonPath is a Gatling check, not an arbitrary JSON parser. You can’t use Gatling components in your own functions. You have to use standard Java libraries.

.check(
  css("meta[name='myConfig.1']", "content")
    .transform(content => yourOwnJsonParsing(yourOwnHtmlUnescaping(content))
    .saveAs("cacheId")

Thank you @slandelle , i’m trying to do this.
I’ll keep you in touch.

OK, i create a package “org.mypackage” and when i run it in java, i still need to include other jars (even if they are included in my jar file “DecodeAndJSONParse” )
The function works as following :

Usage: java -cp "bin;lib/*" org.mypackage.DecodeAndJSONParse encodedJson jsonPath"

As when i tryed the
However, with the bundle, and the scala code :

import org.mypackage

.....
.....

			.check(css("meta[name='my.Config.1']", "content")
				.transform(content => org.mypackage.DecodeAndJSONParse(content, "$..cacheId")//.saveAs("contentValue")
				)
				.saveAs("contentValue")
			)

i have the error

class org.mypackage.DecodeAndJSONParse is not a value
.transform(content => org.mypackage.DecodeAndJSONParse(content, “$…cacheId”)//.saveAs(“contentValue”)

and when i check at your advice :

.check(
  css("meta[name='myConfig.1']", "content")
    .transform(content => yourOwnJsonParsing(yourOwnHtmlUnescaping(content))
    .saveAs("cacheId")

even if i changed my function, it should have worked, isn’t it?..

Hello,

i checked again, and it was not working as expected due to the fact that I created a main program and not a simple library.
That’s ok now.

Finally that’s :

			.check(css("meta[name='myConfig.1']", "content")
				.transform(content => org.mypackage.DecodeAndParse.getValue(DecodeAndParse.unescapeHtml(content),"$...cacheId") //.saveAs("contentValue")
				.replace("[", "").replace("]", "").replaceAll("\"", "") // Cleaning contentValue
				)
				.saveAs("contentValue")

Thank you @slandelle .

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.