Access weird JSON Format - javascript

I am using an API that returns the following JSON:
I want to access the data in "value", but I have no idea how to do it. I don't want to mess around too often, because the API has a limited amount of credits and costs money.
EDIT: Sample JSON:
{
"method": [
["somename"]
],
"answer": [{
"name": [{
"domain": "somedomain",
"date": "somedate",
"value": "somenumber"
}]
}],
"credits": [{
"used": 1
}]
}
EDIT2: I honestly don't get why this is being downvoted.

The correct path is :
json.answer[0].name[0].value

You will get value by
jsonData.answer[0].name[0].value

Try this:
jsonData[1].answer[0][0].value

{
"method": [
["somename"]
],
"answer": [{
"name": [{
"domain": "somedomain",
"date": "somedate",
"value": "somenumber"
}]
}],
"credits": [{
"used": 1
}]
}
answer would be obj.answer[0].name[0].value

Related

Microsoft power automate: JSON "Object reference not set to an instance of an object"

I am attempting to create a JSON Object from an array to pass into a Microsoft product. The format in which the JSON object is accepted is shown beneath (content-type: "application/json"):
{
"value": [
{
"activityGroupNames": [],
"confidence": 0,
"description": "This is a canary indicator for demo purpose. Take no action on any observables set in this indicator.",
"expirationDateTime": "2019-03-01T21:44:03.1668987+00:00",
"externalId": "Test--8586509942423126760MS164-0",
"fileHashType": "sha256",
"fileHashValue": "b555c45c5b1b01304217e72118d6ca1b14b7013644a078273cea27bbdc1cf9d6",
"killChain": [],
"malwareFamilyNames": [],
"severity": 0,
"tags": [],
"targetProduct": "Azure Sentinel",
"threatType": "WatchList",
"tlpLevel": "green",
},
{
"activityGroupNames": [],
"confidence": 0,
"description": "This is a canary indicator for demo purpose. Take no action on any observables set in this indicator.",
"expirationDateTime": "2019-03-01T21:44:03.1748779+00:00",
"externalId": "Test--8586509942423126760MS164-1",
"fileHashType": "sha256",
"fileHashValue": "1796b433950990b28d6a22456c9d2b58ced1bdfcdf5f16f7e39d6b9bdca4213b",
"killChain": [],
"malwareFamilyNames": [],
"severity": 0,
"tags": [],
"targetProduct": "Azure Sentinel",
"threatType": "WatchList",
"tlpLevel": "green",
}
]
}
I making use of an inline code script in Microsoft automate that performs the following in JavaScript:
var threat = workflowContext.actions.Compose.outputs;
var value = Object.values(threat);
return value;
The workflowContext.actions.Compose.outputs line pulls an array consisting of objects shown in the following snippet:
[{"id": "1", "activityGroupNames": "test2"}, {"id": "2", "activityGroupNames": "test3"}, {"id": "3", "activityGroupNames": "test4"}]
This is my output:
{
"body": [
{
"id": "1",
"action": "alert",
"activityGroupNames": "test2"
},
{
"id": "2",
"action": "alert",
"activityGroupNames": "test3"
},
{
"id": "3",
"action": "alert",
"activityGroupNames": "test2"
}
]
}
it is pretty much identical to the format described my Microsoft shown in the first snippet. (https://learn.microsoft.com/en-us/graph/api/tiindicator-submittiindicators?view=graph-rest-beta&tabs=http) at the bottom.
I am unsure as to how I can change the key name from "body" to "value" and think maybe this will resolve my issue. Either way, I'd appreciate any other help on the matter, if any more context is required, please ask.
EDIT: The image beneath shows that the returned return value; is in fact being used as the input for a POST request to the Microsoft graph API

Ember Difference between Restadapter vs Jsonapiadapter

Ember uses Restadapter & Jsonapiadapter for the adapters.
What are the exact differences between the 2 in terms of data formats for request/response ?
Any other things we need to ensure when using any of these 2.
The JSONAPIAdapter conforms to the JSONApi spec
Use RESTAdapter when you have an JSON API that follows a REST endpoint with pluralized object names and has a root node using the name of the object being returned.
Examples below:
Example JSONAPI spec object:
{
"data": [{
"type": "articles",
"id": "1",
"attributes": {
"title": "JSON API paints my bikeshed!"
},
"relationships": {
"author": {
"links": {
"self": "http://example.com/articles/1/relationships/author",
"related": "http://example.com/articles/1/author"
},
"data": { "type": "people", "id": "7" }
}
},
}],
"included": [{
"type": "people",
"id": "7",
"attributes": {
"name": "Dave",
"twitter": "kiwiupover"
}
}]
}
Example Rest json api object:
{
"posts": {
"id": 5,
"title": "An API that gets bikeshed for months ",
"author": "kiwiupover",
"comments": [1]
},
"comments": [{
"id": 1,
"name": "Dave",
}]
}
Ember Data provides straightforward methods for adapting your DS.adapter to your specific JSON API shape.
There is a third adapter from which the previously mentioned adapters are extended from.

Dojox Json Query expressions

I am using Dojox to get an array of objects back that meet an expression, I recently changed my service and now it returns more complicated JSON property banes and my query has stopped working. I think I can place a wild card [*]operator to the start of my query string.
Old JSON
[
{
"attributes": [{
"Type": 1,
"User": "Dave",
"Location": "England"
}]
},
{
"attributes": [{
"Type": 1,
"User": "Paul",
"Location": "England"
}]
},
{
"attributes": [{
"Type": 2,
"User": "James",
"Location": "England"
}]
}
]
Old query string
var jam = dojox.json.query(“[?attributes.Type='0'”, data);
This used to work fine but since the JSON has changed the query isn't working
New JSON
[
{
"attributes": [{
somevalue."sometingelse".username.Type: 1,
somevalue."sometingelse".username.User: "Dave",
somevalue."sometingelse".username.Location: "England",
}]
},
{
"attributes": [{
somevalue."sometingelse".username.Type: 1,
somevalue."sometingelse".username.User: "Paul",
somevalue."sometingelse".username.Location: "England",
}]
},
{
"attributes": [{
somevalue."sometingelse".username.Type: 2,
somevalue."sometingelse".username.User: "Steve",
somevalue."sometingelse".username.Location: "England",
}]
}
]
I have tried with a few variations of a similar query with no success
var jam = dojox.json.query(“[?attributes.*Type='0'”, data);
var jam = dojox.json.query(“[?attributes][*Type='0']”, data);
Happy to give further details, thank you in advance.
Essentially a dodgy join we replaced it with a view and set the field names. Another fix was to rename all the layers in the MXD and republished the ArcGIS service.
var jam = dojox.json.query(“[?attributes.Type='0'”, data);
Would work fine after that.

Iterate through nested Javascript Objects from API response

I've tried 100 different things, and spend days looking through Google and Stackoverflow, but I can't find a solution to this problem. Everything I call after the body of this API response returns undefined!
The response from Facebook SDK looks like this:
[
{
"body": "[
"data": [
{
"name": "Larry Syid Wright",
"administrator": false,
"id": "xxx"
}, {
"name": "Melissa Long Jackson",
"administrator": false,
"id": "xxx"
}, {
"name": "Charlotte Masson",
"administrator": false,
"id": "xxx"
}
],
"paging": {
"next": "url"
}
]"
},{
"body": "{
"data": [
{
"id": "xxx_xxx",
"message": "In honor of Halloween, how many of you have your own ghost stories? Who believes in ghosts and who doesn't?",
"type": "status",
"created_time": "2014-10-31T20:02:01+0000",
"updated_time": "2014-11-01T02:52:51+0000",
"likes": {
"data": [
{
"id": "xxx",
"name": "Joe HerBatman Owenby Jr."
}
],
}
"paging": {
"cursors":
{
"after": "xxx",
"before": "xxx"
}
}
}
},{
"id": "xxx_xxx",
"from": {
"id": "xxx",
"name": "Jessica Starling"
},
"message": "Watching the "Campaign" and I can't help but notice what a fantastic job they did (Will ferrell and all) with that North Carolina accent! Ya'll know we sound different than other southern states ;)",
"type": "status",
"created_time": "2014-11-01T02:36:21+0000",
"updated_time": "2014-11-01T02:36:21+0000",
"likes": {
"data": [
{
"id": "xxx",
"name": "Scott Williams"n
}
]
}
}
],
"paging": {
"previous": "xxx",
"next": "xxx"
}
}"
}
]
This response is from a batch call. If I call them separately, I can easily iterate through the responses, and get everything from them. When I call them in the batch though, I can't get past "body", and I need to use a batch call.
console.log(response[0].body); will return the object inside the body of the first part of the response, but console.log(response[0].body.data); returns undefined. I just don't get it. This should be simple but it's like there's a lock on the door and I don't have the right key.
I normally have no issue iterating through objects, so I don't need a generalized answer. I need help seeing whatever it is here that I don't see. Why does the console show undefined when I call anything after the body, and what do I need to be doing to get any of these values?
That JSON contains nested JSON. body seems to be a string. Use
var body = JSON.parse(response[0].body);
The values from the body are just strings.which are embedded as json.So firstly you would need to parse them using JSON.parse.
The code would be like
var body = JSON.parse(response[0].body);

JSON.parse returning "scanEscape a"

I have QML LocalStorage using javaScript. In it I put object via JSON.stringify(). When I try to read the object from DB using JSON.parse() is returning : scanEscape a and I did't found a reference to this scan error JSON file:
{
"header": {
"version": "2.5",
"createdIn": "PickWorks - Linux",
"modifiedIn": "PickWorks.appName",
"modified": "2013/12/07"
},
"properties": {
"title": "We found a love",
"authors": [
{
"name": "Rihana"
}
],
"transposition": -2,
"tempo": {
"type": "bpm",
"value": "130"
},
"key": "C",
"version": "2.5.4",
"publisher": "GuitarTab",
"keywords": [
"find",
"love",
"deny"
],
"verseOrder": "v1 b c v2 b c",
"themes": [
"love",
"hopeless"
]
},
"lyrics": [
{
"title": "v1",
"text": "[a]Yellow diamonds [F]in the light\n[C]And we're standing [G]side by side\n[a]As your shadow [F]crosses mine\n[C]What it takes to [G]come [a]alive.[F]\n",
"items": {}
},
{
"title": "v2",
"text": "[a]Shine a light through [F]an open door\n[C]Love and life [G]I will divide\n[a]Turn away cause [F]I need you more\n[C]Feel the heart-[G]beat in my [a]mind.[F]\n"
},
{
"title": "c",
"text": "[a]We found love in a [F]hopeless place\n[C]We found love in a [G]hopeless place\n[a]We found love in a [F]hopeless place\n[C]We found love in a [G]hopeless place\n"
},
{
"title": "b",
"text": "[C]It's the way I'm feeling [G]I just can't [a]deny.[F]\n[C]But I've gotta [G]let it go\n"
}
]
}
Q1: What is this error ?
Q2: How to solve it?
P.S.: (Tested on Qt 5.2b & 5.1.1)
Bug was actually some missed debugging log in Qt. Upon creating Bugtrack issue problem was adressed and will be solved in next stable release (Qt 5.2.0).

Categories