parsing JSON object to get filelocation - json

i am new to JSON. Here i am facing problem how to get file location from JSON object, i am using javascript to parse the json
{
"_embedded": {
"binaries": [
{
"fileLocation": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
"username": "testuser3",
"description": "The company required the 28-year-old's help on a matter the directors felt could affect the share price: its Wikipedia page. Short, uninteresting .",
"createdAt": "2015-02-01T21:47:07.000+0000",
"updatedAt": "2015-02-01T22:42:16.000+0000"
},
{
"fileLocation": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
"username": "Sumanth",
"description": "Sample",
"createdAt": "2015-02-23T21:37:13.000+0000",
"updatedAt": "2015-02-23T21:43:11.000+0000"
},
{
"fileLocation": "http://images.clipartpanda.com/sports-equipment-clipart-black-and-white-soccer-ball-hi.png",
"username": "as",
"description": "as",
"createdAt": "2015-02-02T22:46:00.000+0000",
"updatedAt": "2015-02-27T22:06:18.000+0000"
}
]
}
}
i want to read all file locations from JSON object .
Can anyone help me. Thanks in advance

The way you are trying to access fileLocation looks right:
data._embedded.binaries[1].fileLocation
Before that, to parse your data use:
parsed = JSON.parse(data);
Here's a fiddle: http://jsfiddle.net/01aL5upc/
Note that I did remove whitespace and the apostrophe in 28-year-old's in the fiddle, to put it in to a string rather than an ajax request.

Related

Should I sent images through JSON

I'm new to Django, and I'm building a REST backend for my iOS app, I figured out encode an Image to it's Base64 data, and I would like to use it together with some texts, is it a good idea to put it into the JSON for the rest of the data or is it better to use another request to download each image data.
I'm guessing that separating concerns is the best way, and have two GET request to retrieve the data, but I just want to be sure!.
OPTION A
{
"owner": {
"username": "lilysmith",
"email": "lily#email.com",
"first_name": "Lily",
"last_name": "Smith"
},
"is_user_page": true,
"title": "Lily Smith",
"subtitle": "Photographer",
"about": [
{
"icon": "🇺🇸",
"label": "Austin, TX"
}
],
"photos": [
{
"position": 2,
"description": "I love hiking",
"photo": "/9j/2wCEAAgGBg....CEAAgGBg" <- (The rest of the binary data)
}
]
}
OPTION B (Separating Concern)
The same JSON response, but instead of image data, using it's id.
Then getting it just the data through another request, but esencially making the front end handle two or more request to server.

Why is JSON being detected as empty?

So I have a JSON file I got from Postman which is returning as an empty object. This is how I'm reading it.
import regscooter from './json_files/reginald_griffin_scooter.json'
const scoot = regscooter;
const CustomerPage = () => {...}
reginald_griffin_scooter.json
{
"success": true,
"result": {
"id": "hhhhhhhhhh",
"model": "V1 Scooter",
"name": "hhhhhhhhhh",
"status": "active",
"availabilityStatus": "not-available",
"availabilityTrackingOn": true,
"serial": "hhhhhhhhhhhh",
"createdByUser": "hhhhhhhhK",
"createdByUsername": "hhhhhhhh",
"subAssets": [
"F0lOjWBAnG"
],
"parts": [
"hhhhhhhh"
],
"assignedCustomers": [
"hhhhhhhhh"
],
"createdAt": "2019-12-03T21:47:26.218Z",
"updatedAt": "2020-06-26T22:05:54.526Z",
"customFieldsAsset": [
{
"id": "hhhhhhh",
"name": "MAC",
"value": "hhhhhhhh",
"asset": "hhhhhhhhhh",
"user": "hhhhhhhhh",
"createdAt": "2019-12-03T21:47:26.342Z",
"updatedAt": "2019-12-11T16:29:24.732Z"
},
{
"id": "hhhhhhhh",
"name": "IMEI",
"value": "hhhhhhh",
"asset": "hhhhhhh",
"user": "hhhhhhhhhh",
"createdAt": "2019-12-03T21:47:26.342Z",
"updatedAt": "2019-12-11T16:29:24.834Z"
},
{
"id": "hhhhhhhhh",
"name": "Key Number",
"value": "NA",
"asset": "hhhhhhhhh",
"user": "hhhhhhhhhhh",
"createdAt": "2019-12-03T21:47:26.342Z",
"updatedAt": "2019-12-11T16:29:24.911Z"
}
]
}
}
The error is that "const scoot" is being shown as an empty object {}. I made sure to save a ton of times everywhere. I am able to read through the imported JSON file in other variables in similar ways, so I don't know why I can't parse this one. I just want to access the JSON object inside this. Also I omitted some information with hhhhh because of confidentiality.
EDIT: The code works, but it still has a red line beneath result when I do:
const scoot = regscooter.result.id;
It would be much more effective if you will provide an example in codesandbox or so.
However at first look it might be a parser issue ( maybe you are using Webpack with missing configuration for parsing files with json extension ), meaning we need more information to provide you with a full answer ( maybe solution ? ).
Have you tried to do the next:
const scoot = require('./json_files/reginald_griffin_scooter.json');

How to read data from nested JSON structure in protractor

Below is my code and I want to extract data under "specs" part like description, status etc. however I'm getting undefined when I capture the data and print it in the console. I have tried
let web = JSON.parse(jsondata);
let TestSuite = web["suite1"]["description"]
and this is providing data in console however, when I use this,
let id = web["suite1"]["specs"]["id"]
its gives undefined. Please help!
{
"suite1": {
"id": "suite1",
"description": "Login",
"fullName": "Login",
"failedExpectations": [],
"status": "finished",
"specs": [
{
"id": "spec0",
"description": "Should able to login into the Distribution management Webpage",
"fullName": "Login Should able to login into the Distribution management Webpage",
"failedExpectations": [
{
"matcherName": "",
"message": "",
"stack": "",
"passed": false,
"expected": "",
"actual": ""
}
],
"passedExpectations": [],
"pendingReason": "",
"started": "2018-09-06T06:57:42.740Z",
"status": "failed",
"duration": "7 secs",
"stopped": "2018-09-06T06:57:49.255Z",
"browserLogs": []
}
]
} }
JSON.parse(JSON.stringify(json)).suite1.specs[0].id
specs contains an array of objects. First, you need to get the object from the array and then get a value of the object.
Try this:
let id = web["suite1"]["specs"][0]["id"]
OR
let id = web.suite1.specs[0].id
Hope this will work.
When you do ["suite1"]["specs"], you select array. You need an index, which is the 0-th one in this case.
You can check that by typing Object.prototype.toString.call( data["suite1"]["specs"]).
You can try with:
data["suite1"]["specs"][0]["id"]
Or use the object property notation
data["suite1"]["specs"][0].id

Parsing through JSON .. Gives undefined?

I have a very complex JSON and a snippet of it is below:
var designerJSON=
{
"nodes":
[
{
"NodeDefinition": {
"name": "Start",
"thumbnail": "Start.png",
"icon": "Start.png",
"info": "Entry point ",
"help": "Start point in your workflow.",
"workflow ": "Start",
"category": "Basic",
"ui": [
{
"label": "Entry point",
"category": "Help",
"componet": "label",
"type": "label"
}
]
},
"States": [
{
"start": "node1"
}
]
},.......
]
}
I would like to get the value of "start" in States. But I am stuck in the first step of entering into JSON. When I try
console.log(designerJSON["nodes"]);
I am getting Undefined.
I want the value of start. Wich is designerJSON["nodes"]["States"]["start"].
Can you help.
Thanks in advance
designerJSON["nodes"]["States"]["start"] won't do it.
designerJSON["nodes"] is a list, as is States, so you need to access individual items by index (or iteration).
In the example you have given you need to use this:
designerJSON['nodes'][0]['States'][0]['start']
or this (cleaner IMO):
designerJSON.nodes[0].States[0].start
You have an array in JSON.
instead of
designerJSON["nodes"]["States"]["start"]
use
designerJSON["nodes"][0]["States"][0]["start"]
ps. pay attention on how code is formatted in the topic.
pps. using brackets for accessing properties in js is "bad style" (due to js hint recommendations). better access those via dot, e.g:
designerJSON.nodes[0].States[0].start

How does one enforce HTTP responses as JSON messages?

I am quite new to json and webservices.
I have a question which might as well be a dumb one but I have not been able to find an answer.
Lets say I have a html form which performs a GET method on submission.
The server performs an HTTP response.
How does one make sure that the content(body) of the response is in JSON format?
-V
This is quite a vague question as I don't know what your server is doing and replying (obviously it should be in the JSON format response) but you should also include a Content-type HTTP header as shown below so the browser knows it is receiving JSON:
Content-type: application/json
Your JSON response should look like this (taken from Wikipedia):
{
"firstName": "John",
"lastName": "Smith",
"age": 25,
"address": {
"streetAddress": "21 2nd Street",
"city": "New York",
"state": "NY",
"postalCode": 10021
},
"phoneNumbers": [
{
"type": "home",
"number": "212 555-1234"
},
{
"type": "fax",
"number": "646 555-4567"
}
]
}
data=response_from_the_page;
try {
json = $.parseJSON(data);
} catch (e) {
// not json
}