Retrieve the value from a JSONP formatted data object - javascript

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.

Related

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.

Can I provide a json path in CSV file in postman

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"
}
]

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.

Socrata map results in $http request, Javascript

I am trying to use this dataset to determine which district boundary an address (passed into the API call) falls within.
The endpoint returns an array of objects for each district or council. The polygon is found within the "the_geom" property, with 2 properties - type and coordinates. I have tried using $where, but I get errors.
[
{
"comments": "Inaugurated 2015-06-22",
"council": "1",
"councilper": "Scott Griggs",
"district": "1",
"objectid": "1",
"shape_area": "343352603.892",
"shape_leng": "88541.3042539",
"the_geom": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-96.80995700065864,
32.77138899977414
],
[
-96.80969800043205,
32.77121999997131
],
[ ...
I tried to use the query below, but it gave me an error:
https://www.dallasopendata.com/resource/h9ws-fqcn.json?$where=within_polygon(the_geom, 'MULTIPOLYGON (((-96.800270, 32.779091)))')
This is the page reference page - https://www.dallasopendata.com/Geography-Boundaries/Adopted-Council-Districts/6dcw-hhpj
And this is the endpoint- https://www.dallasopendata.com/resource/dgxr-hmze.json
any help would be greatly appreciated.
I suspect you're the developer who popped into our IRC channel, but I'll answer here too!
You're pretty close here! What you want to do here is use the intersects(...) SoQL function with a Well Known Text (WKT) POINT.
Here's an example that works for your use case:
https://www.dallasopendata.com/resource/h9ws-fqcn.json?$where=intersects(the_geom,%20%27POINT%20(-96.7994007%2032.775765)%27)

How Do I See the Output of Changes to JSON

Updated to try to be more clear given the comments (Thank you for comments)
I apologize in advance for this question. I may just not have the vocabulary to properly research it. If I have an array of objects called restaurants stored in my project, for example:
restaurants: [
{
"name": "joes's pizza",
"url": "joespizza.com",
"social properties": {
"Facebook":"fb.com/pizza",
"Instagram":"instagram.com/pizza",
"googlePlus":"pizza+",
"Twitter":"twitter.com/pizza"
}
},
{
"name": "tony's subs",
"url": "tonys.com",
"social properties": {
"Facebook":"fb.com/subs",
"Instagram":"instagram.com/subs",
"googlePlus":"subs+",
"Twitter":"twitter.com/subs"
}
},
{....}
]
I then run a function to add a unique idea to all the objects in the array. The result of console.log(restaurants) is this:
{
"id": 3472,
"name": "joes's pizza",
"url": "joespizza.com",
"social properties": {
"Facebook":"fb.com/pizza",
"Instagram":"instagram.com/pizza",
"googlePlus":"pizza+",
"Twitter":"twitter.com/pizza"
}
},
{
"id": 9987,
"name": "tony's subs",
"url": "tonys.com",
"social properties": {
"Facebook":"fb.com/subs",
"Instagram":"instagram.com/subs",
"googlePlus":"subs+",
"Twitter":"twitter.com/subs"
}
},
{....}
]
I would now like to have this updated array of objects available to look at in my project, via the text editor, as a variable or restaurants.json file. How do I actually see the new modified json array and how do i save it so that i can work with it the same way i did this one above? I am currently doing this in the browser. I can see the results if i log it to the console but I need to be able to work with the new output. Thanks for taking the time to answer.
You can encode/decode JSON with JSON.stringify() and JSON.parse().
Aside from converting to/from JSON, you work with standard JS objects and arrays:
var array = JSON.parse(json_str);
array[0].address = "5th Avenue";
console.log(JSON.stringify(array));
Well, there's really not enough information in your question but I assume a few things:
You've loaded the json data from somewhere and it has been turned into a javascript object.
You've edited the object somehow and wish to convert it back to json and save the changes.
Assuming the above to be true, you just need to serialize the object back to json and submit it back to your server where you can save it in any manner you deem appropriate.
You can serialize the javascript object with JSON.stringify() (see https://stackoverflow.com/a/912247/4424504)
Add the serialized json to a hidden field on the form and submit it.
On the server when processing the form submission, grab the data from the hidden field and do with it what you wish.
Or get it back to the server any way you wish (ajax call, whatever) the key point is to serialize the object to a json string and save it
Hope that helps...

Categories

Resources