how to declare two arrays in json? - json

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.

Related

Validate nested response body rest assured

I know that there is a lot of information regarding validations in rest assure but unfortunately I am not able to find exactly the solution to my issue.
I am training to validate the response of the following JSON:
{
"A":[
{
"B":[
{
"C":"c",
"D":"d"
}
],
"E":[
{
"F":[
{
"G":"g1"
}
]
},
{
"F":[
{
"G":"g2"
}
],
},
{
"F":[]
}
]
}
],
"H": "h"
}
with the following code:
response.body("A.E.F.G", hasItems(expectedValues.get(G).toArray(new String[0])))
expectedValues.get(G) return a list of g1 and g2.
the ERROR that I get is:
JSON path A.E.F.G doesn't match.
Expected: an array containing ["g1", "g2"]
Actual: [[[g1], [g2], []]]
How can I get that done?
That solve the problem:
response.body("A.E.F.G.flatten()", hasItems(expectedValues.get(G).toArray(new String[0])))

convert a JSON to another w/ circe

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.

How do i iterate json object

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..
}

Nested JSON Format

I have a data Called 'Village'.
Country:
{
State:{
District:{
Taluk:{
Village: String
}
}
}
}
Is this correct JSON format for Data "Village".
Following is the modified json to make it valid
{
"Country": {
"State": {
"District": {
"Taluk": {
"Village": "String"
}
}
}
}
}
and to validate any json string use http://jsonlint.com/
You don't declare types such as String in JSON. Also, you need to quote your variable names. It seems like your declaring a structure in JSON. I would replace String with an empty string, that is essentially declaring the type:
{"Country":
{
"State":{
"District":{
"Taluk":{
"Village": ""
}
}
}
}
}

Jsonpath for a JSON payload

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