I would like to convert this JSON
{
"l1k1": {
"l2k1": "l2v1",
"l2k2": 1
},
"l1k2": [
{
"e1l1": "e1v1",
"e1l2": "e1v2"
},
{
"e2l1": "e2v1",
"e2l2": "e2v2"
}
]
}
to this one
{
"papa": {
"l1k1c": {
"l2k1c": {
"string": "l2v1"
},
"l2k2c": {
"int": 1
}
},
"l1k2c": {
"array": [
{
"e1l1": "e1v1",
"e1l2": "e1v2"
},
{
"e2l1": "e2v1",
"e2l2": "e2v2"
}
]
}
}
}
where:
"l" stands for level
"k" for key, "v" for value
"e" for element
"c" for copy (where "*" maps to "*c")
I'm using circe's Json but having a hard time renaming the keys or creating parents or children with it. As I'm writing this, I'm thinking I may need to use its ACursor instead. As you may have guessed, I'm trying to generate an AVRO doc from an input JSON. I'm open to help w/ my approach or any suggestions about how to go about it in a cleaner way.
Related
I have a scenario where I want to populate attribute based on data value.
Input :
{
"PersonName":{
"FirstName":"03-03-1984",
"ID":"123567",
"Name":[
"Betty Lou",
"Tre Chernockxdev"
],
"LastName":"Tre Chernockxdev"
}
}
Output:
{
"Birth":{
"YearOfBirth":"1984"
},
"Employee":{
"LastName":"Tre Chernockxdev"
},
"Element":{
"Category":"Fixed"
}
}
So, based on ID value I want to populate Gender attribute. If ID is number then populate Category with Fixed or if ID is alphanumeric then populate category with Floating value.
Jolt Spec:
[
{
"operation":"shift",
"spec":{
"PersonName":{
"FirstName":{
"*-*-*":{
"$(0,3)":"Birth.YearOfBirth"
}
},
"LastName":"Employee.LastName",
"ID":"Fixed|Floating"
}
}
}
]
I also checked jolt string function but could not find a function which can be used to check the data category. Is their way where I can find whether ID value contains number or alphanumeric? Or can I apply the regex pattern to check whether data contains a number or alphanumeric value?
You want to achieve something like ternary operator which is hard to get but possible:
[
{
"operation":"shift",
"spec":{
"PersonName":{
"*":"&"
}
}
},
{
"operation":"modify-overwrite-beta",
"spec":{
"ID":[
"=toInteger",
"Floating"
]
}
},
{
"operation":"shift",
"spec":{
"ID":{
"#":"ID",
"Floating":null,
"*":{
"#Fixed":"ID"
}
},
"*":"&"
}
},
{
"operation":"modify-overwrite-beta",
"spec":{
"ID":"=lastElement(#(1,ID))"
}
},
{
"operation":"shift",
"spec":{
"FirstName":{
"*-*-*":{
"$(0,3)":"Birth.YearOfBirth"
}
},
"LastName":"Employee.LastName",
"ID":"Element.Category"
}
}
]
See also:
Jolt Object functions
I have a JSON like below: I need to extract the Options -> Child as a Random and also Values within the options as randomly. How can we achieve in jmeter ?
{
"id":37,
"merchant_id":"39",
"title":"Parker Pens",
"subtitle":null,
"price":1000,
"description":null,
"images":[ ],
"image_thumbs":[ ],
"options":[
{
"code":"color",
"label":"Color",
"extra_info":"",
"values":[
{ },
{ },
{ }
]
},
{
"code":"size",
"label":"Size",
"extra_info":"",
"values":[
{ },
{ },
{ }
]
}
],"options_available":[
{ },
{ },
{ },
{ },
{ },
{ },
{ },
{ },
{ }
], "custom_options":[
]
}
I have to fetch the child of options randomly . In that i have to fetch the value of "Code" and its associated value within the "Value" .
Help is appreciated and useful
Your requirements are a little bit vague as you haven't indicated what is the desired output format. One of the solutions would be using JSR223 PostProcessor in order to obtain the random value from random options array like:
import com.jayway.jsonpath.JsonPath
import org.apache.commons.lang3.RandomUtils
import org.apache.jmeter.samplers.SampleResult
def options = JsonPath.read(prev.getResponseDataAsString(), '$.options')
def randomOption = options.get(RandomUtils.nextInt(0, options.size()))
def values = randomOption.get('values')
def randomValue = values.get(RandomUtils.nextInt(0, values.size())) as String
vars.put('randomValue', randomValue)
References:
Jayway JsonPath - A Java DSL for reading JSON documents
Apache Groovy - Why and How You Should Use It
Apache Groovy - Parsing and Producing JSON
this json formate have basically two array object so i am lit bit confused that how do i iterate these two object and display separately.
[
{
"todays_birthday":[
{
"member_id":"1",
"member_name":"Subhash Chander Chandna",
"member_number":"490",
"member_dob":"1995-06-30"
},
{
"member_id":"4",
"member_name":"Mrs. Bimla Rani",
"member_number":"500",
"member_dob":"1946-06-30"
}
]
},
{
"tommorow_birthday":[
{
"member_id":"3",
"member_name":"Jagdish Kumar Pahuja",
"member_number":"490",
"member_dob":"1946-07-01"
},
{
"member_id":"5",
"member_name":"Raja Ram",
"member_number":"500",
"member_dob":"1946-07-01"
}
]
}
]
I think your json could be formed as an object with two properties (today_birthdays and tomorrow_birthdays) and inside each property you get the array of birthdays.
Like that:
{
today_birthdays: [
//birthdays
],
tomorrow_birthdays: [
//birthdays
]
}
And so you could iterate through the birthdays, for example, with lodash (in JavaScript):
_(obj.today_birthdays).forEach(function(birthday) {
//use birthday var..
}
I have the following JSON payload. I would like to extract the value "value_for_key_attribute_Y" using JSON path expression in a generic manner (i.e. without hard coding any array values like [1]) Any ideas ?
{
"requests":[
{
"event":[
{
"parameter":"parameter_key_A",
"event":"event_key_A",
}
],
"data":[
{
"id":"id_xyz",
"payload_data":[
{
"key":"key_attribute_X",
"value":"value_for_key_attribute_X",
},
{
"key":"key_attribute_Y",
"value":"value_for_key_attribute_Y",
}
]
}
]
}
]
}
Something like this should work for you.
$.requests[*].data[*].payload_data[?(#.key_path == 'key_attribute_Y')].value
I want to create a json string for my web application. Actually i am new to this json format.In my json string i have to create two arrays in my json structure.But i have some syntax problem in creating two arrays. my json string is given below for your reference.
{
MarkUpdate:[
{
'FinalMarks':[
{
'studentId':'S1',
'Ques_Mark':[
{
'qId' :'Q1',
'mark':'14',
},
{
'qId':'Q2',
'mark':'10',
}
]
},
{
'studentId':'S2',
'Ques_Mark':[
{
'qId' :'Q1',
'mark':'12',
},
{
'qId':'Q2',
'mark':'13',
}
]
}
]
}
]
}
In my above json string format,my MarkUpdate contains one array object named as FinalMarks.So,here i have to create one more array object named as EvalMarks under MarkUpdate.
Acually my EvalMarks contains following elements...
'EvalMarks':[
{
'EvalId':'E1',
'Ques_Mark':[
{
'qId' :'Q1',
'studId':'S1',
'mark':'13',
},
{
'qId':'Q2',
'studId':'S1',
'mark':'13',
}
]
},
{
'EvalId':'E2',
'Ques_Mark':[
{
'qId' :'Q1',
'studId':'S2',
'mark':'10',
},
{
'qId':'Q2',
'studId':'S2',
'mark':'10',
}
]
}
]
So, i have declare this EvalMarks under the MarkUpdate.I missed the syntax...
Could you plz tell me how to add this array object under the MarkUpdate.
guide me to get out of this issue...
To declare two arrays in one JSON object, remember that the JSON object can only be a single object, therefore the array must be inside the enclosing curly braces. For example:
{
"array1":[1,2,3],
"array2":["jim","louise","mark"]
}
For your case, it's important to remember that you should properly indent your braces, square and curly, so that you can visually identify mistakes before they become problems. I stringly recommend http://jslint.com/ to validate your JSON before using it. It's also great for Javascript:
{
"MarkUpdate":[
{
"FinalMarks":[
{
"studentId":"S1",
"Ques_Mark":[
{
"qId" :"Q1",
"mark":"14"
},
{
"qId":"Q2",
"mark":"10"
}
]
},
{
"studentId":"S2",
"Ques_Mark":[
{
"qId" :"Q1",
"mark":"12"
},
{
"qId":"Q2",
"mark":"13"
}
]
}
]
}
],
"EvalMarks":[
{
"EvalId":"E1",
"Ques_Mark":[
{
"qId" :"Q1",
"studId":"S1",
"mark":"13"
},
{
"qId":"Q2",
"studId":"S1",
"mark":"13"
}
]
},
{
"EvalId":"E2",
"Ques_Mark":[
{
"qId" :"Q1",
"studId":"S2",
"mark":"10"
},
{
"qId":"Q2",
"studId":"S2",
"mark":"10"
}
]
}
]
}
Its not a valid JSON if you have commas after the last key-value pair in an object. I would start by knocking off all those unnecessary commas after the last key-value pairs inside most of your objects and validating the JSON in www.jslint.com
To be more clear ,
{
"qId":"Q2",
"studId":"S2",
"mark":"10",
}
is Not Valid.
On the other hand,
{
"qId":"Q2",
"studId":"S2",
"mark":"10"
}
Is valid.