How can I get only the JSON keys without using tilde sign? - json

I am trying to get (via JSON Path) only key values from the JSON below but with no success.
I cannot use the tilde sign (~) because JMeter's JSON Path Extractor works under JSON PATH 4.0 and ~ is not recognized.
{
"facetCount": {
"designer": {
"4856430": 2,
"7313551": 14,
"7018102": 8,
"306838": 1,
"85146": 146,
"2654979": 11,
"221111": 4,
"180510": 40,
"3344622": 59,
"472718": 73,
"107993": 19,
"166170": 58,
"6908": 2,
"426629": 1,
"1358858": 9,
"9879178": 6,
"55006": 43,
"285396": 2,
"3355": 9,
"215501": 8,
"4968477": 4,
"11349629": 7,
"11229643": 27,
"11355128": 9,
"7093068": 3,
"11098281": 2,
"5833751": 1,
"4741301": 1,
"9198104": 21,
"991324": 4
},
"attributes": {
"135979:77": 290,
"135979:83": 27,
"136227:20": 141,
"136227:78": 670,
"135985:44": 123,
"135985:43": 669,
"135979:62": 700,
"135979:61": 1188,
"136644:176": 2,
"136331:7": 1,
"136331:8": 3,
"136641:190": 13,
"136641:191": 12,
"136061:144": 3
},
"category": {
"136103": 208,
"136105": 147,
"137322": 2,
"136389": 120,
"136215": 236,
"136214": 954,
"136216": 217,
"136217": 352,
"136218": 452,
"136219": 40,
"136480": 4,
"136220": 111,
"136221": 288,
"136222": 58,
"136223": 369,
"136224": 163,
"136986": 3,
"136307": 1125,
"136059": 10,
"136308": 956,
"136315": 984,
"136003": 574,
"136045": 267,
"136035": 1501,
"135985": 1380,
"137134": 27,
"136309": 60,
"137323": 9,
"136390": 1,
"136021": 16,
"136322": 1951,
"137166": 16,
"137317": 7,
"136005": 4,
"135983": 4019,
"136033": 1513,
"136310": 1224,
"136392": 18,
"135981": 2430,
"136031": 16,
"136326": 1312,
"136061": 79
},
"colour": {
"1": 41686,
"7": 14593,
"5": 9596,
"18": 1,
"13": 5185,
"6": 5259,
"3": 6391,
"11": 5715,
"12": 1537,
"4": 8767,
"16": 1466,
"9": 8590,
"15": 1730,
"8": 8333,
"14": 3208,
"2": 13269,
"10": 2730
},
"ninetyminutes": {
"3": 309
},
"sameday": {
"3": 1714,
"42": 254
},
"size": {
"135972:1620": 523,
"136657:2650": 1,
"136657:2850": 1
},
"location": {
"3": 2674,
"4": 7671,
"5": 35808,
"6": 2761,
"7": 11948
},
"labels": {
"1300": 2969
}
}
}
I would like to get the keys that are under facetCount element (designers, attributes, colour, etc.) and also (another JSON Path expression) get the keys that are inside these keys, such as 4856430 from designers, 135979:77 from attributes, and so on.
Could you help me, please?
Thanks in advance!

You can use JSR223 PostProcessor to get key in object JSON
new groovy.json.JsonSlurper().parse(prev.getResponseData()).facetCount.eachWithIndex{ def node, int idx ->
log.info('Key ' + idx + '=' + node.getKey())
vars.put('key_' + idx, node.getKey())
}
And you can get key with a variable like that:
${key_0}, ${key_1}, ...
More detail check here How to extract values from json in jmeter when the keys are unkown?

JMeter's JSON test elements rely on Jayway Jsonpath which doesn't has this tilde operator for querying the keys, you will have to go for:
JSR223 PostProcessor
Groovy language
JsonSlurper
Add JSR223 PostProcessor as a child of the request which returns the above JSON and use the following code:
For direct keys (designer, attributes, etc)
def counter = 1
new groovy.json.JsonSlurper().parse(prev.getResponseData()).facetCount.each { facet ->
vars.put('key_' + counter, facet.key)
counter++
}
vars.put('key_matchNr', counter - 1 as String)
For child keys (4856430, 135979:77, etc)
def counter = 1
new groovy.json.JsonSlurper().parse(prev.getResponseData()).facetCount.each { child ->
child.value.keySet().each { key ->
vars.put('childKey_' + counter, key)
counter++
}
}
vars.put('childKey_matchNr', counter -1 as String)
More information: Groovy - Parsing and producing JSON

Related

Take a value 5 and convert it to json object

i should take a JSON value, like
{
"cnt": 5
}
and create JSON object from JSONata, like
{
"1": 1,
"2": 4,
"3": 9,
"4": 16,
"5": 25,
}
I tried to do that, but i cant join two expressions correctly.
{
"" & [1..cnt] : [1..cnt].($*$)
}
and i get as a result:
{
"[1,2,3,4,5]": [
1,
4,
9,
16,
25
]
}
Can anybody help me with this(
Try [1..cnt]{$string($): $*$}
See https://try.jsonata.org/SVapg4fzt

JSON equivalent of XML

I want to create JSON file equivalent to below mentioned XML file
<Data>
<EE id="1001">
<a1>50</a1>
<a2>100</a2>
<a3>25</a3>
<a4>10</a4>
<a5>1</a5>
<a6>1</a6>
</EE >
<EE id="1002">
<a1>75</a1>
<a2>60</a2>
<a3>35</a3>
<a4>20</a4>
<a5>1</a5>
<a6>1</a6>
</EE >
<EE id="1003">
<a1>100</a1>
<a2>80</a2>
<a3>50</a3>
<a4>40</a4>
<a5>10</a5>
<a6>10</a6>
</EE >
</Data>
What will be the JSON equivalent of above xml?
Also please share the best Guide/Tutorial on JSON
{
"1001": {
"a": [
50,
100,
25,
10,
1,
1
]
},
"1002": {
"a": [
75,
60,
35,
20,
1,
1
]
},
"1003": {
"a": [
100,
80,
40,
50,
10,
10
]
}
}

How do I determine the column type name from the columnType integer value in mysql2?

When querying with mysql2, the third argument to the callback function fields has the following documentation:
console.log(fields); // fields contains extra meta data about results, if available
Ok, great. But when I look at the values in the fields array, I see the following:
[
{
"_buf":{},
"_clientEncoding":"utf8",
"_catalogLength":3,
"_catalogStart":10,
"_schemaLength":0,
"_schemaStart":14,
"_tableLength":11,
"_tableStart":15,
"_orgTableLength":0,
"_orgTableStart":27,
"_orgNameLength":2,
"_orgNameStart":31,
"characterSet":63,
"encoding":"binary",
"name":"id",
"columnLength":11,
"columnType":3, // <-- column type 3? This is an INTEGER field
"flags":1,
"decimals":0
},
{
...
"columnType":253, // <-- column type 253? This is a VARCHAR field
...
}
]
How do I determine what the actual column type is from these integer values?
After doing some more digging, I was able to find the answer by looking at the source. mysql2 exposes a Types field that has the column type names as keys with the column id as values.
At the time of posting, that list looks like this:
{
"DECIMAL": 0,
"TINY": 1,
"SHORT": 2,
"LONG": 3,
"FLOAT": 4,
"DOUBLE": 5,
"NULL": 6,
"TIMESTAMP": 7,
"LONGLONG": 8,
"INT24": 9,
"DATE": 10,
"TIME": 11,
"DATETIME": 12,
"YEAR": 13,
"NEWDATE": 14,
"VARCHAR": 15,
"BIT": 16,
"JSON": 245,
"NEWDECIMAL": 246,
"ENUM": 247,
"SET": 248,
"TINY_BLOB": 249,
"MEDIUM_BLOB": 250,
"LONG_BLOB": 251,
"BLOB": 252,
"VAR_STRING": 253,
"STRING": 254,
"GEOMETRY": 255
}

ignore the value of IntSequenceGenerator in JSON response

I have used #JsonIdentityInfo on my class to avoid Infinite Recursion as follows
#JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class, property = "id", scope = IseidaPerfile.class)
public class IseidaPerfile implements Serializable {}
It is fixed the issue and my JSON response looks goods
{
"id": 22,
"id": 3,
"literal": "Responsible",
"permissions": [
3,
4,
{
"id": 23,
"id": 6,
"literal": "Show menu"
},
7,
8,
9,
10,
11,
12,
13,
14,
15,
16,
17,
18,
19
]
}
But I want to ignore the unused ids from Permissions as 3, 4,7, ..... 18, 19? which generated by IntSequenceGenerator

How could I merge 2 json into one? do I need to merge them as strings?

Here is my code for merging 2 jsons into one... Is there a better way?
Expected behaviour is to get a 'big json' with sum of 'entry jsons' and no
"type-XX" overlaps, and keeping the longest 'rc[ ]' list.
def json_src1 = '''
{
"branch": {
"type-0.2": {
"version": "0.2",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
}
}
'''
def json_src2 = '''
{
"branch": {
"type-0.3": {
"version": "0.3",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
}
}
'''
def concatenateJsons(json_src1, json_src2){
def json = json_src1 + json_src2
return json
}
To change strings to json objects:
json_src1 = new JsonSlurper().parseText(json_src1)
json_src2 = new JsonSlurper().parseText(json_src2)
To concatenate
json = concatenateJsons(json_src1, json_src2)
println JsonOutput.prettyPrint(JsonOutput.toJson(json))
Expected result:
{
"branch": {
"type-0.2": {
"version": "0.2",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
"type-0.3": {
"version": "0.3",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
}
}
P.S I am losing order of elements inside of Json after merge for no obvious reason :
("version": "0.3" moves to the bottom in type-03 {}:
Instead of:
{
"branch": {
"type-0.2": {
"version": "0.2",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
"type-0.3": {
"version": "0.3",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
}
}
I am getting
{
"branch": {
"type-0.2": {
"version": "0.2",
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]"
}
"type-0.3": {
"rc": "[17, 18, 19, 20, 21, 22, 23, 24]",
"version": "0.3"
}
}
}
the simplest thing you can do is to change the concatenateJsons function
def concatenateJsons(json_src1, json_src2){
def json = [branch: json_src1.branch + json_src2.branch]
return json
}
groovy not doing deep merge of parsed jsons it merges only root objects.
so, to merge content of branches just sum branch from each json
Note: if both json1 and json2 has the same type-X.Y then second overlaps content of first one.