Can I provide a json path in CSV file in postman - javascript

I was trying to execute a postman collection that has many iteration. Data was provided in CSV format.
In each iteration i want to compare different nodes with expected messages provided in the datafile, for example,
sample collection:
{
"message": [{
"code": XYX,
"abc": "how are you",
"def": "where are you from"
"ghi": "your name",
"jkl": "place"
}]
}
in 1st iteration - my test is as below
pm.response.json().message[0].code.to eql(data.ExpectedErrorMessage);
2nd iteration,
pm.response.json().message[0].abc.to eql(data.ExpectedErrorMessage);
and so on.
I have 100 + nodes and many arrays in the list. so its not feasible to write test for each node separately. Instead, i want to write a generic test and keep the response path in CSV file.
I tried by providing the path in below way in my data file.But i am unable to perform my test. I am getting the assertion error
[
{
"TC_ID": "TC_001",
"TC_NAME": "abc",
"path": "pm.response.json().message[0].code",
"ExpectedErrorMessage": "XYZ"
},
{
"TC_ID": "TC_002",
"TC_NAME": "def",
"path": "pm.response.json().message[0].abc",
"ExpectedErrorMessage": "how are you"
}
]

Related

For Prisma Client join queries is it possible to move deeply nested fields to top level of result?

Does Prisma have the ability to move nested fields from another table join to the top level of the result, like a flattened view? I want to put the result JSON into a frontend table without digging through nested objects and building another object.
For example I want to replicate this behavior where I can pick and choose the columns from different tables (columns from User, and School). Currently I use a raw query with a similar SQL, however I wonder if it's possible with using only the Prisma API:
SELECT
u.id
, u.email
, s.school_name
FROM "User" AS u
JOIN "UserSchool" AS us ON us.user_id = u.id
JOIN "School" AS s ON s.id = us.school_id
id | email | school_name
123| student1#email.com | mount high
I want JSON that looks like this:
{
"id": "1",
"email": "student1#email.com",
"school_name": "mount high",
}
If I did this in Prisma, I would need to go into several levels of nested objects to get the same column name on another table for e.g. user[user_school][schoo][school_name]. This requires extra work to loop through all my results, extract from the nested object, and build another object. This example isn't too bad, but I have more joins and deeply nested objects for my actual problem (lots of association/lookup tables). I've experimented with the select and include for my joins, but they are structured with the nested JSON.
users = await prisma.user.findMany({
include: {
user_school: {
include: {
school: true,
},
},
},
{
"id": "1",
"email": "student1#email.com",
"user_school": [
{
"id": 1,
"user_id": "1",
"school_id": "1",
"school": {
"id": 1,
"school_name": "mountain high",
}
}
],
}
It's not possible to flatten out the results as of now.
The only way to achieve this would be by using rawQuery as you mentioned.
However, there is an existing Feature Request which discusses Flattening out the results by providing an option of flatten:true. If you could mention your use case there and add a comment it would help Prisma's Product and engineering team in prioritising it.

Pull a full object from one value

I'm working on a ticket system, and I want to make a close ticket system, that will remove the ticket from the user, so I have my mongo array looking like this
{
"tickets": [{
"id": "cxZgqey2",
"subject": "fdgfdgdfgfdgfd",
"message": "gfdgfdgfdgdfg",
"ownerEmail": "soenmtherhg#gmail.com"
},
{
"id": "r4r-CnIC",
"subject": "dfdsfdsfsdfdsfdsf",
"message": "dsfdsfdfsdfdsfdsfdsfdsf",
"ownerEmail": "soenmtherhg#gmail.com"
}
]}
and I wanted to remove the entire object using only the id, how would I do this? (Using the npm module (not mongoose))
You can use the filter function in arrays and delete it passing the relevant id for that and also you can check loadash.

How to get back valid JSON from mysql column where a javascript object is stored

I have some data I need to store inside a mysql table. The table has a few columns, among them a book and authors column, both of which are TEXT fields. I need to store a javascript object in the book column and an array of objects in the authors column.
Inside my angular component, I have the book object and authors array of objects.
const book = {
title: "hello world",
subtitle: "learn C programming in a week",
cover: "path/to/cover.png"
};
const authors = [{id: 3, name: "John Doe"}, {id: 4, name: "Jane Doe"}];
The object I'm passing to my service to handle the post request looks something like the following.
{
id: this.generateId(),
category: this.category,
book: JSON.stringify(book),
authors: JSON.stringify(authors),
price: this.price
}
Both of these are being stringified via JSON.stringify(...) and then successfully being posted to the database. No problem there.
I should also mention that the object values are being grabbed and put into an SQL statement in the POST request like so...
`INSERT INTO library (id, category, book, authors)
VALUES ('${obj['id']}',
'${obj['category']}',
'${obj['book']}',
'${obj['authors']}')`
However, when I try to retrieve data from that table via my angular service, angular throws the error Http failure during parsing for http://localhost:4200/.
When I fire the same request via postman, I get back the data but then when I check the validity of the returned JSON, it's not valid. The returned data looks something like this...
The title" gets highlighted with the error Expecting comma, colon or }, not string
[
{
"book": "{
"title": "hello world",
"subtitle": "learn C programming in a week",
"cover": "path/to/cover.png"
}",
"authors": "[{"id": "3", "name": "John Doe"}, {"id": "4", "name": "Jane Doe"}]"
}
]
What am I missing? How do I get back valid JSON?
"book": "{ - this looks like you have stringyfied the two values separately and then added to one object?
If so create one object first and stringify the entire thing.

Retrieve the value from a JSONP formatted data object

Just when I think I've got he hang of identifying an element in an object, I run into a scenario that I cannot seem to get the value I want.
This part works and the data returned is correct: I have a map and when I attempt to identify a building on the map, I receive the following json object (this has been shortened for readability but in real life, its properly formatted): The function MapClick(queryResults) is called when the map is clicked.
dojo.io.script.jsonp_dojoIoScript19._jsonpCallback({
"results": [
{
"layerId": 5,
"layerName": "Building",
"value": "Name of item clicked",
"displayFieldName": "name",
"attributes": {
"ID": "123",
"name": "Name of item clicked",
"Variable1": "Some bit of information",
"Variable2": "Some other bit of information",
...
...
All I'm trying to do is return either the results[0].value OR results[0].attributes.name which in this example should bring back "Name of item clicked". The layerId, layerName, value, and displayFieldName are the "common most accessed data" so they are returned but the same information is also found within the attributes.
I've tried console.log(results[1].attributes.name); and console.log(results) with no success.
Turns out the name of the function handling the MapClicked is queryResults was needed so the correct answer is: queryResults[0].value and when you see open brackets [, you can be sure you will need [ some number ] (e.g. queryResults[0].value or queryResults[99].someothervariable. Or at least I think this is a correct statement.

How to translate Solr JSON response into HTML while JSON is different every time

I am using Solr 4 for searching in a java web application.Solr produces a JSON response from which i have to extract search results and translate them into html so user can read that.
I know one solution but it seems dumb an I think there must be intelligent ideas.
{
"responseHeader": {
"status": 0,
"QTime": 0,
"params": {
"fl": "id,title",
"indent": "true",
"q": "solr",
"wt": "json"
}
},
"response": {
"numFound": 3,
"start": 0,
"docs": [
{
"id": "1",
"title": "Solr cookbook"
},
{
"id": "2",
"title": "Solr results"
},
{
"id": "3",
"title": "Solr perfect search"
}
]
}
}
After that i eval this text as:
var obj = eval ("(" + txt + ")");
To generate html page i can use either
<script>
document.getElementById("id").innerHTML = obj.response.docs[1].id
document.getElementById("title").innerHTML = obj.response.docs[1].title
</script>
or
document.write(obj.response.docs[1].id);
But limitation is that every time solr gives response with different object structure i.e. an object may have age feild but other can not have because it depends on query.
I want to use a sigle JSP page to display search results(like Google)
for all search queries
is it possible to write a single code segment which works for any possible search results with different schema.
Javascript stops working after encountering any error which is likely in my case. that's also problem.if I use for loop to traverse the object hierarchy it is highly error -prone.
Is it possible with a single view page Thanks.
You might want to consider using ajax-solr - A JavaScript framework for creating user interfaces to Solr
I suggest using Velocity templating which is readily supported in Solr - instead of extracting data from the JSON and rendering the HTML via JS.
Docs here

Categories

Resources