This is response data from server
[{"username": "harry"}][{"id": 1, "name": "playlist1", "tag": "genre"}, {"id": 2, "name": "playlist1", "tag": "genre"}, {"id": 3, "name": "playlist3", "tag": "genre"}][{"1": ["https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af"]}, {"2": ["https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af", "https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af"]}, {"3": ["https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af", "https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af"]}]
With axios, data comes as a string in the format as above.
There are three arrays in the above string, and I want to extract each one separately and store it in a variable.
It doesn't converted to JSON. How can I get the data of username and the data of the second array? I tried to access it by index, but since it is a string format, it is accessed one by one letter
You should get response fixed as sugguested by #certainperformance and #ambianbeing, but if you can't and still want to extract data from this string response you can do it by doing something like this
const axiosResponse = `[{"username": "harry"}][{"id": 1, "name": "playlist1", "tag": "genre"}, {"id": 2, "name": "playlist1", "tag": "genre"}, {"id": 3, "name": "playlist3", "tag": "genre"}][{"1": ["https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af"]}, {"2": ["https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af", "https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af"]}, {"3": ["https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af", "https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9", "https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af"]}]`;
const result = JSON.stringify(
JSON.parse('{ "data": [' + axiosResponse.split('][').join('],[') + ']}').data[1]
);
console.log(result);
The response you have posted, is not a string. It appears to be three arrays, each containing x amount of objects.
A string can be identified, by being enclosed in either double: " " or single qoutes: ' '.
You can identify the arrays by the opening and closing square brackets: [ ].
The objects can be identified by the opening and closing curly brackets: { }.
Here is a bit more readable version of your response. I have annotated each top level array, with a comment, for easier identification.
// array 1
[
{"username": "harry"}
]
// array 2
[
{ "id": 1, "name": "playlist1", "tag": "genre" },
{ "id": 2, "name": "playlist1", "tag": "genre" },
{ "id": 3, "name": "playlist3", "tag": "genre" }
]
// array 3
[
{
"1": [
"https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9",
"https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af"
]
},
{
"2": [
"https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9",
"https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af",
"https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9",
"https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af"
]
},
{
"3": [
"https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9",
"https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af",
"https://i.scdn.co/image/ab67616d0000b273503143a281a3f30268dcd9f9",
"https://i.scdn.co/image/ab67616d0000b27369fa55f10c5293bbb985c1af"
]
}
]
The third array appears to contain an object, with a list within it. Like somebody else mentioned earlier, this seems like faulty data, and I would have a look at the source, instead of trying to parse it. That being said, it isn't impossible to loop through each list, and extract the data, but it wouldn't be the correct way of doing it.
Related
[
{
"id": "628ba44f5a6de600071d16fa",
"#baseType": "LogicalResource",
"isBundle": false,
"isMNP": false,
"businessType": [],
"category": [
{
"id": "628ba3ef5a6de600071d165f",
"name": "Starterpack2",
"description": "Starterpack2",
"code": "RC17",
"version": 2
}}]
now i need to check and print the JSON Object inside the JSON Array if category is present then it should print and in future if category is changed according to that if we pass parameter the output should print we don't hard code the code
i have tried by using key values it is coming but if the key value changes it is not printing the object
EX:-
[
{
"id": "628ba44f5a6de600071d16fa",
"#baseType": "LogicalResource",
"isBundle": false,
"isMNP": false,
"businessType": [],
"category": [
{
"id": "628ba3ef5a6de600071d165f",
"name": "Starterpack2",
"description": "Starterpack2",
"code": "RC17",
"version": 2
}}]
in the above code i have printed category object but if category changed to categories it is not printing so i want a code which can read the code and based on parameters user giving it should be print the output
Try this.
For Example:
let a = [{"id": "628ba44f5a6de600071d16fa","category": [
{
"id": "628ba3ef5a6de600071d165f",
"name": "Starterpack2",
"description": "Starterpack2",
"code": "RC17",
"version": 2
}]}]
function print (values){return (a[0][`${values}`])}
//now just pass any name like "category" or in future "categories"
print("category") //this will retrun the array.
Now modify with your requirements.
It seems you want to get the value of the key(that can be parameterized).
const jsonArray = [
{
"id": "628ba44f5a6de600071d16fa",
"#baseType": "LogicalResource",
"isBundle": false,
"isMNP": false,
"businessType": [],
"category": [
{
"id": "628ba3ef5a6de600071d165f",
"name": "Starterpack2",
"description": "Starterpack2",
"code": "RC17",
"version": 2
}
]
}
];
const parameter = "category";
const result = jsonArray.find(({ [parameter]: value }) => value);
if (result) {
console.log(result);
} else {
console.log(`No object found with ${parameter}`);
}
If this is not what you are looking for, then please add your code snippet for better understanding.
I'm trying to store in MongoDB one document with an object with the properties I want to map latter. My idea it's to create a function that will receive 2 params. First the object where I got to find the mapping, and second the object where I have to take the info from.
For example I want to store this JSON (that would be the first parameter in the function):
{
"name": "client.firstName",
"surname": "client.surname",
"age": "client.age",
"skills": [
{
"skillName": "client.skills[index].name",
"level": "client.skills[index].levelNumber",
"categories": [
{
"categoryName": "client.skills[index].categories[index].name",
"isImportant": "client.skills[index].categories[index].important"
}
]
}
]
}
And the second paramenter would be something like this (it's the object where you find the information.
{
"client": {
"firstName": "Jake",
"surname": "Long",
"age": 20,
"skills": [
{
"name": "Fly",
"level": 102,
"categories": [
{
"name": "air",
"important": true
},
{
"name": "superpower",
"important": false
}
]
},
{
"name": "FastSpeed",
"level": 163,
"categories": [
{
"name": "superpower",
"important": false
}
]
}
]
}
}
The idea it's: with de paths that I have in the first object, find it in the second one.. The problem I found it's when I have arrays, because when I defined the mapping rules I don't know how many positions will have the array I want to map. So in the mapping object (first) I'll only define the path but I'll not put it with the same lenght of the secondone because I don't know how much it will have.
I am working with facebook JS SDK which returns user's information in JSON format. I know how to get the response like response.email which returns email address. But how to get an element from a nested array object? Example: user's education history may contain multiple arrays and each array will have an element such as "name" of "school". I want to get the element from the last array of an object.
This is a sample JSON I got:-
"education": [
{
"school": {
"id": "162285817180560",
"name": "Jhenaidah** School"
},
"type": "H**hool",
"year": {
"id": "14404**5610606",
"name": "2011"
},
"id": "855**14449421"
},
{
"concentration": [
{
"id": "15158**968",
"name": "Sof**ering"
},
{
"id": "20179020**7859",
"name": "Dig**ty"
}
],
"school": {
"id": "10827**27428",
"name": "Univer**g"
},
"type": "College",
"id": "9885**826013"
},
{
"concentration": [
{
"id": "108196**810",
"name": "Science"
}
],
"school": {
"id": "2772**996993",
"name": "some COLLEGE NAME I WANT TO GET"
},
"type": "College",
"year": {
"id": "1388*****",
"name": "2013"
},
"id": "8811215**16"
}]
Let's say I want to get "name": "some COLLEGE NAME I WANT TO GET" from the last array. How to do that with Javascript? I hope I could explain my problem. Thank you
Here is a JsFiddle Example
var json = '{}' // your data;
// convert to javascript object:
var obj = JSON.parse(json);
// get last item in array:
var last = obj.education[obj.education.length - 1].school.name;
// result: some COLLEGE NAME I WANT TO GET
If your json above was saved to an object called json, you could access the school name "some COLLEGE NAME I WANT TO GET" with the following:
json.education[2].school.name
If you know where that element is, then you can just select it as already mentioned by calling
var obj = FACEBOOK_ACTION;
obj.education[2].school.name
If you want to select specifically the last element, then use something like this:
obj.education[ obj.education.length - 1 ].scool.name
Try this,
if (myData.hasOwnProperty('merchant_id')) {
// do something here
}
where JSON myData is:
{
amount: "10.00",
email: "someone#example.com",
merchant_id: "123",
mobile_no: "9874563210",
order_id: "123456",
passkey: "1234"
}
This is a simple example for your understanding. In your scenario of nested objects, loop over your JSON data and use hasOwnProperty to check if key name exists.
Trying to get all JSON objects in an array. It's only returning the last one.
Here's a sample of my JSON:
{
"manufacturer":{
"name": "manufacturername",
"cameras": [
{
"name": "sdfsdfsd",
"type": "Audio device",
"resolution": "Unknown",
"channels": "1"
}
]
},
"manufacturer":{
"name": "manufacturername2",
"cameras": [
{
"name": "sdfsdf",
"type": "Camera",
"resolution": "720P/1.3MP",
"channels": "2"
},
{
"name": "D12",
"type": "Camera",
"resolution": "1080P/3MP",
"channels": "1"
}
]
}}
It is valid JSON.
Here's how I'm calling it:
//Get Manufacturer data
$http.get('data2.json').success(function(data) {
$scope.maninfo = data;
console.log($scope.maninfo);
});
The actual array is much longer - and it's just returning the last Object for some reason.
What you have is valid JSON, but it doesn't correctly express your intent. "manufacturer" (or "name", or "cameras") isn't a type name, it's a unique key into a collection of named values -- dictionary, map, hash, whatever(1). JSON data structures are just a subset of JavaScript object literal declarations (hence the name: JavaScript Object Notation).
So the example above is not an array, it's two successive value assignments to the "manufacturer" property of the same parent object. The parser is assigning the first one to the "manufacturer" property, then replacing that with the second (and in your original, larger) "array", it's then replacing that with the third, and so on.
The "cameras" properties in the manfacturer objects are properly functioning arrays. Just do the same at the higher level -- something more like this:
{
"manufacturers":
[
{
"name": "manufacturername",
"cameras": [
{
"name": "sdfsdfsd",
"type": "Audio device",
"resolution": "Unknown",
"channels": "1"
}
]
},
{
"name": "manufacturername2",
"cameras": [
{
"name": "sdfsdf",
"type": "Camera",
"resolution": "720P/1.3MP",
"channels": "2"
},
{
"name": "D12",
"type": "Camera",
"resolution": "1080P/3MP",
"channels": "1"
}
]
}
]
}
(1) Dictionary, map, hash -- or "associative array". But I didn't want to call it any kind of "array" in that paragraph, because the whole point is it's not the other kind of array.
I am having problem with my data.
My JSon looks like that:
[
{
"link": {
"created_at": "2013-10-07T13:31:43+09:00",
"id": 8,
"items_count": 4,
"key": "0iqVSnTU-BtJ1ItVKRe2VMWvRMU",
"mode": "standard",
"name": "sdasadads",
"pusher_key": "1jtsrzl3n6i1DKA3tSZJM6LPnfQ",
"readonly_key": "R_dD5oHMsruu0YzYVKEOA8hKKXA-r",
"updated_at": "2013-10-08T14:06:07+09:00",
"user_id": 2
}
},
{
"link": {
"created_at": "2013-10-07T13:32:56+09:00",
"id": 9,
"items_count": 1,
"key": "Mj-6Cc-_qaGlVTPgqKexzeijYNA",
"mode": "standard",
"name": "Untitled2",
"pusher_key": "hGE0D8TSar_H_Gv9MWdpj26gamM",
"readonly_key": "T53SNKPgyf7KvRUMzDQPaM99AAc-r",
"updated_at": "2013-10-07T13:33:14+09:00",
"user_id": 2
}
},
{
"link": {
"created_at": "2013-10-11T11:18:06+09:00",
"id": 10,
"items_count": 0,
"key": "X_ZoKxFPHtsvSU5W11gXx1653FU",
"mode": "standard",
"name": "Usdadasas",
"pusher_key": "0PZ860awofRKB9XIrXba-xY6u14",
"readonly_key": "2rzrRZAaR7UZRK3UbUId8xmFzd4-r",
"updated_at": "2013-10-11T11:18:06+09:00",
"user_id": 2
}
}
}
I am trying to print put all the names of the links like that:
$.post( "http://0.0.0.0:9292/api/links", function( data ) {
document.getElementById("test").innerHTML = data[link][0].name;
});
but it doesn't work.
How can I grub all the names and put it in html?
The objects are inside the array, not the other way around.
link is a literal property name, not a variable containing one as a string
Thus:
data[0]['link']['name']
You'll also need to make sure that the response has an application/json content type.
Grabbing all the names will require you to use a loop and change the 0 each time round it.
First of all change last "}" to "]" (your top structure is array not object )
Then try this
$.post( "http://0.0.0.0:9292/api/links", function( data ) {
document.getElementById("test").innerHTML = data[0].link.name;
});
Array.prototype.map() is a good way to fetch something from data structure. With your test data in data variable, could would look like this:
Example here.
<div class="names"></div>
var names = data.map(function (item) {
return item.link.name
});
document.querySelector(".names").innerHTML = names;