Parse information from JSON with EJS - json

How do I access only certain parts of JSON response? Is this even possible?
I get the whole response to show up on my EJS template as:
{ "result": { "price": { "last": 2086.004, "high": 2228.857, "low": 1980, "change": { "percentage": -0.048247334, "absolute": -105.74609 } }, "volume": 11616.328 }, "allowance": { "cost": 4279664, "remaining": 3766767537 } }
With:
var rows =<%-JSON.stringify(data)%>
document.getElementById("1").innerHTML = rows;
But how would I access, let’s say result.price.last?
I've tried all kind of approaches, but none seem to work.

As #Andrew Li proposed, rows.result.price.last, I could swear I tried it at least twice, Oh well, it works now.

Related

Querying in elasticsearch to get a json object as output

{
"key":
[
{
"text": "I noticed when you came up he's even",
"start": 0.0,
"duration": 4.68
},
{
"text": "playing with a little bit your your",
"start": 3.3,
"duration": 5.07
}
]
}
I have huge amount of files in this format and i have to write a query for only getting the text
and its start.But,I'm getting the whole file in which the text is present as output.
if i write the query like this:
{
"query":
{
"term":
{
"key.text": "came"
}
}
}
then i should get only
{
"text": "I noticed when you came up he's even",
"start": 0.0,
"duration": 4.68
}
as output but Instead im getting the whole file as output.
You need to use the inner hits of nested query to fetch only the item in a array matches your search query.

replace "key" name in whole JSON python for bulk data in efficient way

Actually i am pushing data to other system but before pushing i have to change the "key" in the whole JSON. JSON may contain 200 or 10000 or 250000 data.
sample JSON:
{
"insert": "table",
"contacts": [
{
"testName": "testname",
"ContactID": 212121
},
{
"testName": "testname",
"ContactID": 2146354564
},
{
"testName": "testname",
"ContactID": 12312
},
{
"testName": "testname",
"ContactID": 211221
},
{
"testName": "testname",
"ContactID": 10218550
}
]
}
I need to change contacts array Keys. These contacts may be in bulk. So i need to work with this efficiently with minimal complexity.
The above JSON to be converted as below
{
"insert": "table",
"contacts": [
{
"name": "testname",
"phone": 212121
},
{
"name": "testname",
"phone": 2146354564
},
{
"name": "testname",
"phone": 12312
},
{
"name": "testname",
"phone": 211221
},
{
"name": "testname",
"phone": 10218550
}
]
}
here is my code trying by loop
ini_dict = request.data
contact_data = ini_dict['contacts']
for i in contact_data:
i['name'] = i.pop('testName')
print(contact_data)
Please suggest me how can i change the key names efficiently for bulk data. i mean for 50000 lists in contacts. "for loop" will be leading a performance issue. So please let me know the efficient way to achieve this
I dont know how fast you need it to be nor how you are choosing to store your json. One simple solution is just store it as a string and then replace all the instances of your attributes.
# Something like this using a jsonstring
jsonstring.replace("'testName':", "'name':")
jsonstring.replace("'ContactId':", "'phone':")
If you want to do this in bulk you, may need to create some batch process to be able to fetch multiple existing records and make changes at once. I have done this before with the java equivalent of https://pypi.org/project/JayDeBeApi/ but, that was more for modifying existing records in a database.

Does this parsing data way really works?

Am reading this "issue" from the guys from adaptive cards, and am trying to use their parsing way in my own project with no results. Here´s the entire thing https://github.com/microsoft/AdaptiveCards/issues/2448 the parsing section is below. Does this implementation really work?
JSON.parse Example
This is an Azure DevOps response where the message property is a JSON-serialized string. In order to access values within the string, we need to use the JSON.parse function in our template.
Data
{
"id": "1291525457129548",
"status": 4,
"author": "Matt Hidinger",
"message": "{\"type\":\"Deployment\",\"buildId\":\"9542982\",\"releaseId\":\"129\",\"buildNumber\":\"20180504.3\",\"releaseName\":\"Release-104\",\"repoProvider\":\"GitHub\"}",
"start_time": "2018-05-04T18:05:33.3087147Z",
"end_time": "2018-05-04T18:05:33.3087147Z"
}
Usage
{
"type": "TextBlock",
"text": "{JSON.parse(message).releaseName}"
}
Resulting In
{
"type": "TextBlock",
"text": "Release-104"
}

SparkPost email not sending

I"m trying to get SparkPost setup for my website but keep encountering problems. I'm sending the proper headers so I know that's the issue and am pretty sure it's something with my json body.
This is the endpoint I'm posting to: https://api.sparkpost.com/api/v1/transmissions?num_rcpt_errors=3
{
"content": {
"from":{
"name":"Testing Email",
"email":"testing#hidden.com"
},
"subject": "Thundercats are GO!!!",
"text": "Sword of Omens, give me sight BEYOND sight"
},
"recipients": [
{
"address": {
"email": "jscotto#hidden.com"
}
}
]
}
The response from the above is this:
{
"results": {
"total_rejected_recipients": 0,
"total_accepted_recipients": 1,
"id": "hidden"
}
}
I don't know what could be causing it to not send because from my knowledge, I've followed every guideline on the documentation.
Turns out the emails were sending, they're just hitting my spam box. I ended up setting up an ip_pool of sa_shared and that solved my issue.

Error in Targeting Company Shares Using JSON

I'm trying to Target company shares (https://developers.linkedin.com/documents/targeting-company-shares) using json with LinkedIn Rest Api.
My JSON object (https://developers.linkedin.com/forum/targeting-example-using-multiple-targeting-parameters-and-json) looks like :
{
"visibility": {
"code": "anyone"
},
"comment": "Targeting Shares",
"share-target-reach": {
"share-targets": {
"share-target": [{
"code": "geos",
"tvalues": {
"tvalue": "as"
}
}, {
"code": "companySizes",
"tvalues": {
"tvalue": "201-500"
}
}]
}
}
}
But this isn't working, i'm getting the following error :
{
"errorCode": 0,
"message": "Malformed json document. Encountered unexpected array.",
"requestId": "DRWYRUVMBJ",
"status": 400,
"timestamp": 1423283491818
}
I'm not able to figure out where's the problem??
I have been playing around with this for awhile now and near as I can tell the below format will work. Basically you have to remove all the arrays, I have tried them at every level and they always cause the malformed json error. I still have no idea how to target multiple sections at once though, since the official documentation on this is very wrong. The only way I figured this part out is by doing a whole lot of guessing.
{
"visibility": {
"code": "anyone"
},
"comment": "Targeting Shares",
"share-target-reach": {
"share-targets": {
"share-target": {
"code": "geos",
"tvalues": {"tvalue": "as"}
}
}
}