Compare JSON key values [duplicate] - json

Like if I have two JSON as below and I want to check the mismatch between those
JSON 1:
{
name:'john',
contact:'123',
country:'america'
}
JSON 2:
{
name:'vishal',
contact:'123',
country:'India'
}
Now it will return me with the mismatch between name and country not only the name?

No this is not supported. We feel this is not needed, because in your regular CI runs you only care if the test passed or failed, and you see the details in the log.
Also note that you can simulate this if you really want using a Scenario Outline: https://stackoverflow.com/a/54108755/143475
Finally, if you care so much about this, kindly contribute code, this is open-source after all.
EDIT: you can easily do this by iterating over keys. Here is the code:
EDIT2: Setting up data via a Background is no longer supported in version 1.3.0 onwards, please look at the #setup tag: https://github.com/karatelabs/karate#setup
Feature:
Background:
* def json1 = { name: 'john', contact: '123', country: 'america' }
* def json2 = { name: 'vishal', contact: '123', country: 'India' }
* def keys = karate.keysOf(json1)
* def data = karate.mapWithKey(keys, 'key')
Scenario Outline: <key>
* match (json1[key]) == json2[key]
Examples:
| data |
And here is the report:

Related

Node JS How to change key names in an object using another object for conversion?

I have these tables on one DB (MySQL) which I need to sync with corresponding tables on another DB (MSSQL), but the field names are different. I was wondering what efficient way there is to convert the names of the fields after fetching the rows so that I could insert them into the other tables.
I was thinking of doing something like this. Make objects where the key is the original table column's names and the value is the destination table column's names:
{
name : UNAME
id : CID
location : LOC
}
And the rows that I fetched and need to insert would look something like this:
{
name: Ethan
id: 1234
location: somewhere1
},
{
name: Jhon
id: 5678
location: somewhere2
}
and then run on these objects and change their key names according to the conversion table, so that I can insert them to the destination table properly.
I can't just insert without field names, as the fields are not in the same order.
How can I do what I've described efficiently? Do you have ideas for better strategies to accomplish this?
thanks a lot!
Sounds about right, how about this:
const converter = {
name : UNAME
id : CID
location : LOC
}
let newData = []
dbResults.forEach(row => {
newData.push({
name: `${row[converter['name']]}`
id: `${row[converter['id']]}`
location: `${row[converter['location']]}`
})
})
EDIT:
Actually looking at the above code there is no need for the converter object:
let newData = []
dbResults.forEach(row => {
newData.push({
name: `${row['UNAME']}`
id: `${row['CID']}`
location: `${row['LOC]}`
})
})

How to retrieve a key value based on another key value form an array object of the response body in karate [duplicate]

I think Karate documentation is great and I have tried to read as much as possible but this one little thing is stumping me right now. I think what I am trying to do is fairly straightforward but I am failing at it miserably. I have a JSON object here based on the example in the docs but slightly modified:
* def cat =
"""
[
{
name: 'Billie',
kittens: [
{ id: 23, nickName: 'Bob' },
{ id: 42, nickName: 'Wild' }
]
},
{
name: 'Billie2',
kittens: [
{ id: 233, nickName: 'Bob2' },
{ id: 422, nickName: 'Wild2' }
]
}
]
"""
All I want to find is the value of nickName when id is 233. (In this example the answer is Bob2)
I tried this:
get[0] cat.kittens[?(#.id==233)]
But I think I am missing something.
What is tripping me is when there are multiple name and kittens, as opposed to a single set in the example given in the documentation website.
I apologize as the answer for this is pretty straightforward, but any hint in the right direction will get greatly appreciated. Thank you!
You are close. When there is an array involved, typically [*] is needed.
* def temp1 = get[0] cats[*].kittens[?(#.id==233)]
* match temp1.nickName == 'Bob2'
Note that this will also work:
* def temp2 = get[0] cats..kittens[?(#.id==233)]
* match temp2.nickName == 'Bob2'

Trying to filter json object for a single value based on the value of other key in Karate API Test Framework

I think Karate documentation is great and I have tried to read as much as possible but this one little thing is stumping me right now. I think what I am trying to do is fairly straightforward but I am failing at it miserably. I have a JSON object here based on the example in the docs but slightly modified:
* def cat =
"""
[
{
name: 'Billie',
kittens: [
{ id: 23, nickName: 'Bob' },
{ id: 42, nickName: 'Wild' }
]
},
{
name: 'Billie2',
kittens: [
{ id: 233, nickName: 'Bob2' },
{ id: 422, nickName: 'Wild2' }
]
}
]
"""
All I want to find is the value of nickName when id is 233. (In this example the answer is Bob2)
I tried this:
get[0] cat.kittens[?(#.id==233)]
But I think I am missing something.
What is tripping me is when there are multiple name and kittens, as opposed to a single set in the example given in the documentation website.
I apologize as the answer for this is pretty straightforward, but any hint in the right direction will get greatly appreciated. Thank you!
You are close. When there is an array involved, typically [*] is needed.
* def temp1 = get[0] cats[*].kittens[?(#.id==233)]
* match temp1.nickName == 'Bob2'
Note that this will also work:
* def temp2 = get[0] cats..kittens[?(#.id==233)]
* match temp2.nickName == 'Bob2'

How to query for Null or Missing Fields in Mysql X DevAPI?

How to query for null or missing field in Mysql X DevAPI?
I tried .find("age IS NULL") and .find("age = null") but both not work.
> db.createCollection('users')
> db.getCollection('users').add({ name: "foo", age: 30 })
> db.getCollection('users').add({ name: "bar", age: null })
> db.getCollection('users').find("age IS NULL")
Empty set (0.0003 sec)
> db.getCollection('users').find("age = null")
Empty set (0.0004 sec)
I'm able to reproduce that with the MySQL Shell (which I guess is what you are using) and with Connector/Node.js. I know for a fact that Connector/Node.js sends the correct datatype to the server (with the Shell you can check that with the --trace-proto option).
Mysqlx.Crud.Find {
collection {
name: "<some_schema>"
schema: "users"
}
data_model: DOCUMENT
criteria {
type: OPERATOR
operator {
name: "is"
param {
type: IDENT
identifier {
document_path {
type: MEMBER
value: "age"
}
}
}
param {
type: LITERAL
literal {
type: V_NULL
}
}
}
}
}
Which means it's some issue in the server.
In this case, looks like that X DevAPI expression is not resulting in the proper SQL query. If you look in the general log, you should see something like
SELECT doc FROM `<some_schema>`.`users` WHERE (JSON_EXTRACT(doc,'$.age') IS NULL)
The problem is that JSON_EXTRACT is returning null (JSON type) and not NULL (SQL "type"), and it is a limitation of the X Plugin.
One way for this to be fixed by the plugin is to replace JSON_EXTRACT() with JSON_VALUE(), which will return the proper NULL value in that case, but I don't know the implications of that.
As a workaround, you can always use
session.sql("select doc from `<some_schema>`.`users` where json_value(doc, '$.age') is null").execute()
In the meantime, I encourage you to report a bug at https://bugs.mysql.com/ using either the MySQL Server: Document Store: X Plugin or the MySQL Server: Document Store: MySQL Shell categories.
Disclaimer: I'm the lead developer of the MySQL X DevAPI Connector for Node.js

Can anyone explain how to get all the mismatch between two responses in karate?

Like if I have two JSON as below and I want to check the mismatch between those
JSON 1:
{
name:'john',
contact:'123',
country:'america'
}
JSON 2:
{
name:'vishal',
contact:'123',
country:'India'
}
Now it will return me with the mismatch between name and country not only the name?
No this is not supported. We feel this is not needed, because in your regular CI runs you only care if the test passed or failed, and you see the details in the log.
Also note that you can simulate this if you really want using a Scenario Outline: https://stackoverflow.com/a/54108755/143475
Finally, if you care so much about this, kindly contribute code, this is open-source after all.
EDIT: you can easily do this by iterating over keys. Here is the code:
EDIT2: Setting up data via a Background is no longer supported in version 1.3.0 onwards, please look at the #setup tag: https://github.com/karatelabs/karate#setup
Feature:
Background:
* def json1 = { name: 'john', contact: '123', country: 'america' }
* def json2 = { name: 'vishal', contact: '123', country: 'India' }
* def keys = karate.keysOf(json1)
* def data = karate.mapWithKey(keys, 'key')
Scenario Outline: <key>
* match (json1[key]) == json2[key]
Examples:
| data |
And here is the report: