React native json response - javascript

I am trying to print json responce in via console.log ,below is my json response but i want each element to print one by one like user.id or product.name
{
"user": [{
"id": "1",
"name": "User 1"
}, {
"id": "2",
"name": "User 2"
}],
"user_pictures": false,
"products": [{
"id": "1",
"name": "test abc"
}, {
"id": "2",
"name": "test abc 1"
}],
"purpose": ""
}
I am trying like this:
responseData.map((item)=>{
console.log(item.user.id)
})
but error is show in console
TypeError: responseData.map is not a function
responseData is the response in json when i call my fetch method

You can access id like this input.user[0].id.
Below is working code:
let input = {
"user": [{
"id": "1",
"name": "User 1"
}, {
"id": "2",
"name": "User 2"
}],
"user_pictures": false,
"products": [{
"id": "1",
"name": "test abc"
}, {
"id": "2",
"name": "test abc 1"
}],
"purpose": ""
};
console.log(input.user[0].id);

responseData is an object... you cant map over an object... responseData has 4 properties... users, user_pictures, products, purpose... of which, responseData.users and responseData.products are arrays that can be mapped over. user_pictures is a boolean and purpose is a string.

Its Works like this:
JSON.parse(responseData).user.map((item)=>{
console.log(item.id)
})
Thank guys

Related

How can I get the sites name data from JSON data

How can I get the sites name data value, with entire Formdata array?
Expected output:
["TEST-TRIAL-001120",
"Mubashir Test Site - 001001",
"TEST-TRIAL-001120",
"TEST SITE -001"]
const Formdata = [{
"id": "6efcf3f7-5d29-4a88-8ce9-346229a86765",
"title": "Deepak test form 2",
"version": "1",
"sites": [{
"id": "d7f2b290-2820-401a-a347-9a4dd05ec02b",
"name": "TEST-TRIAL-001120"
}],
"status": "Not Linked"
},
{
"id": "89e5a12d-913d-4f5d-a030-b172af9e27f0",
"title": "Deepak Test form 1",
"version": "1",
"sites": [],
"status": "Not Linked"
},
{
"id": "79a15768-762e-4ff8-b565-31274a38b22d",
"title": "1.7 Form",
"version": "1",
"sites": [{
"id": "32369d1d-f247-4a75-8c14-490c8393dbb8",
"name": "Mubashir Test Site - 001001"
}, {
"id": "d7f2b290-2820-401a-a347-9a4dd05ec02b",
"name": "TEST-TRIAL-001120"
}, {
"id": "a6526163-3ed7-4125-8f32-e0066fc6fe24",
"name": "TEST SITE -001"
}],
"status": "Not Linked"
}
]
Formdata.map(form => {
console.log(form.sites); //not understand how to get sites name data value
});
map over the site array to get an array of names, then flattening that array.
const Formdata=[{id:"6efcf3f7-5d29-4a88-8ce9-346229a86765",title:"Deepak test form 2",version:"1",sites:[{id:"d7f2b290-2820-401a-a347-9a4dd05ec02b",name:"TEST-TRIAL-001120"}],status:"Not Linked"},{id:"89e5a12d-913d-4f5d-a030-b172af9e27f0",title:"Deepak Test form 1",version:"1",sites:[],status:"Not Linked"},{id:"79a15768-762e-4ff8-b565-31274a38b22d",title:"1.7 Form",version:"1",sites:[{id:"32369d1d-f247-4a75-8c14-490c8393dbb8",name:"Mubashir Test Site - 001001"},{id:"d7f2b290-2820-401a-a347-9a4dd05ec02b",name:"TEST-TRIAL-001120"},{id:"a6526163-3ed7-4125-8f32-e0066fc6fe24",name:"TEST SITE -001"}],status:"Not Linked"}];
const result = Formdata.flatMap(({ sites }) => {
return sites.map(({ name }) => name);
});
console.log(result);
Additional documentation
Destructuring assignment
I use the flatMap and map
const Formdata = [{ "id": "6efcf3f7-5d29-4a88-8ce9-346229a86765", "title": "Deepak test form 2", "version": "1", "sites": [{ "id": "d7f2b290-2820-401a-a347-9a4dd05ec02b", "name": "TEST-TRIAL-001120" }], "status": "Not Linked" }, { "id": "89e5a12d-913d-4f5d-a030-b172af9e27f0", "title": "Deepak Test form 1", "version": "1", "sites": [], "status": "Not Linked" }, { "id": "79a15768-762e-4ff8-b565-31274a38b22d", "title": "1.7 Form", "version": "1", "sites": [{ "id": "32369d1d-f247-4a75-8c14-490c8393dbb8", "name": "Mubashir Test Site - 001001" }, { "id": "d7f2b290-2820-401a-a347-9a4dd05ec02b", "name": "TEST-TRIAL-001120" }, { "id": "a6526163-3ed7-4125-8f32-e0066fc6fe24", "name": "TEST SITE -001" }], "status": "Not Linked" } ];
const sites = Formdata.flatMap(({sites}) => sites.map(({name}) => name))
console.log(sites)
First map the sites names from each form. This leaves you with an array of arrays which you need to flatten, which can be done with concat or flatMap
const Formdata = [{"id": "6efcf3f7-5d29-4a88-8ce9-346229a86765","title": "Deepak test form 2","version": "1","sites": [{"id": "d7f2b290-2820-401a-a347-9a4dd05ec02b","name": "TEST-TRIAL-001120"}],"status": "Not Linked"},{"id": "89e5a12d-913d-4f5d-a030-b172af9e27f0","title": "Deepak Test form 1","version": "1","sites": [], "status": "Not Linked"},{"id": "79a15768-762e-4ff8-b565-31274a38b22d","title": "1.7 Form","version": "1","sites": [{"id": "32369d1d-f247-4a75-8c14-490c8393dbb8","name": "Mubashir Test Site - 001001"}, {"id": "d7f2b290-2820-401a-a347-9a4dd05ec02b","name": "TEST-TRIAL-001120"}, {"id": "a6526163-3ed7-4125-8f32-e0066fc6fe24","name": "TEST SITE -001"}],"status": "Not Linked"}]
let sites = [].concat.apply([],
Formdata.map(data =>
data.sites.map(site => site.name)
)
)
/*let sites = Formdata.map(data =>
data.sites.map(site => site.name)
).flatMap(x=>x)*/
console.log(sites)

Reverse an array of object?

How can i reverse this array using react native ? i tried myarray.reverse(); but I get below error message.
TypeError: undefined is not an object (evaluating 'myarray.reverse')
const myarray =
Array [
Object {
"key": "-LrcB3Xcb4QuMtd20TSn",
"value": Object {
"question": "26",
"timestamp": 1571558541642,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
Object {
"key": "-LrcB3pf0DQuCr_vfP_2",
"value": Object {
"question": "27",
"timestamp": 1571558542861,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
]
try to remove Array and Object from the JSON. Works perfect for me.
const myarray = [{
"key": "-LrcB3Xcb4QuMtd20TSn",
"value": {
"question": "26",
"timestamp": 1571558541642,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
{
"key": "-LrcB3pf0DQuCr_vfP_2",
"value": {
"question": "27",
"timestamp": 1571558542861,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
]
myarray.reverse()
Is the pasted code what you are actually getting back from the API?
The formatting/syntax is not correct. As others have stated you shouldn't have the 'Array' and 'Object' words inline like that. I think you're copying code from a terminal/console that added those words in.
If the response actually looks like this you won't have any problem using myaray.reverse()
const myarray = [{
"key": "-LrcB3Xcb4QuMtd20TSn",
"value": {
"question": "26",
"timestamp": 1571558541642,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
{
"key": "-LrcB3pf0DQuCr_vfP_2",
"value": {
"question": "27",
"timestamp": 1571558542861,
"user": "9jNkvzr0chgPi0SC6rXMlVWdOF12",
},
},
]

Checking a value in a nested JSON using Postman

I have a nested JSON returned from an API that I am hitting using a GET request, in POSTMAN chrome app. My JSON looks like this
"result": [
{
"_id": "some_id",
"name": "India",
"code": "IN",
"link": "http://www.india.info/",
"closingTime": "2017-02-25T01:12:17.860Z",
"openingTime": "2017-02-25T06:12:17.205Z",
"image": "image_link",
"status": "online",
"serverStatus": "online",
"games": [
{
"_id": "some_game_id1",
"name": "Cricket"
},
{
"_id": "some_another_id1",
"name": "Baseball"
},
{
"_id": "some_another_id_2",
"name": "Basketball"
}
]
},
{
"_id": "some_id",
"name": "Australia",
"code": "AUS",
"link": "https://www.lonelyplanet.com/aus/adelaide",
"closingTime": "2017-02-28T05:13:38.022Z",
"openingTime": "2017-02-28T05:13:38.682Z",
"image": "some_image_url",
"status": "offline",
"serverStatus": "online",
"games": [
{
"_id": "some_game_id_2",
"name": "Cricket"
},
{
"_id": "some_another_id_3",
"name": "Kho-Kho"
},
{
"_id": "some_another_id_4",
"name": "Badminton"
},
{
"_id": "some_another_id_5",
"name": "Tennis"
}
]
},
I am trying to test whether my response body has "name":"India" and the "game" with "some_game_id1" contains the "name":"cricket".
I went through this link where the answer is to have an array for "name"created and then check within the array whether the array contains the value. I tried this but my code fails.
Also, I tried searching the element by the index within the JSON body using this -
var searchJSON = JSON.parse(responseBody);
tests["name contains India"] = searchJSON.result.name[0]==="India";
But this also fails. I tried using the .value appended with the second line of above code, but it also fails. How can I check this thing?
You need to put [0] after result (which is an array) rather than name (which is a string).
Also, use a regular expression to check whether the name contains 'India', because using === only checks if the name is exactly India.
var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)
Demo Snippet:
var responseBody = `{
"result": [{
"_id": "some_id",
"name": "India",
"code": "IN",
"link": "http://www.india.info/",
"closingTime": "2017-02-25T01:12:17.860Z",
"openingTime": "2017-02-25T06:12:17.205Z",
"image": "image_link",
"status": "online",
"serverStatus": "online",
"games": [{
"_id": "some_game_id1",
"name": "Cricket"
},
{
"_id": "some_another_id1",
"name": "Baseball"
},
{
"_id": "some_another_id_2",
"name": "Basketball"
}
]
},
{
"_id": "some_id",
"name": "Australia",
"code": "AUS",
"link": "https://www.lonelyplanet.com/aus/adelaide",
"closingTime": "2017-02-28T05:13:38.022Z",
"openingTime": "2017-02-28T05:13:38.682Z",
"image": "some_image_url",
"status": "offline",
"serverStatus": "online",
"games": [{
"_id": "some_game_id_2",
"name": "Cricket"
},
{
"_id": "some_another_id_3",
"name": "Kho-Kho"
},
{
"_id": "some_another_id_4",
"name": "Badminton"
},
{
"_id": "some_another_id_5",
"name": "Tennis"
}
]
}
]
}`
var tests = {}
var searchJSON = JSON.parse(responseBody)
tests["name contains India"] = /India/.test(searchJSON.result[0].name)
console.log(tests) //=> { "name contains India": true }

Filtering JSON with underscore

Given a data structure like so:
"items":
{
"Groups":[
{
"title":"group 1",
"SubGroups":[
{
"title":"sub1",
"id" : "1",
"items":[
{
"title":"Ajax request 1",
},
{
"title":"Ajax request 2",
}
]
},
{
"title":"sub2",
"id" : "2",
"items":[
{
"title":"Ajax request 3",
},
{
"title":"Ajax request 4",
}
]
}
]
}
]
How can I pull out all the items for a sub group based on the id? Ive treid using find like so:
var res1 = _.where(listing.items,{id:"2"});
but get an empty array returned
Thanks
Try targeting the subgroups array and then search for the id you want in there. That should then return the properties for that subgroup.
var obj = {
"Groups": [{
"title": "group 1",
"SubGroups": [{
"title": "sub1",
"id": "1",
"items": [{
"title": "Ajax request 1",
}, {
"title": "Ajax request 2",
}]
}, {
"title": "sub2",
"id": "2",
"items": [{
"title": "Ajax request 3",
}, {
"title": "Ajax request 4",
}]
}]
}]
}
then find values like this
_.where(obj.Groups[0].SubGroups, {
'id': '2'
});
Just tested and this seems to work

Constructing multidimensional array out of other arrays

I’m using an external service to pull the events of an organisation (GetEvents). Example of what is returned:
{
"Result":"success",
"Key":"12345",
"Data":[
{"ID":"GFDCV34","lastChangedDate":"2015-12-03 11:14:27"},
{"ID":"IDJHE23","lastChangedDate":"2015-12-03 15:17:47"},
{"ID":"KDJBD34","lastChangedDate":"2015-12-03 05:25:11"}
]
}
Next, I can pull details of a certain event (GetEventDetails). The ID of the event is required as data parameter. I made a function getdetails(id); that returns the details.
For example, for getdetails('KDJBD34'), it gives me:
{
"Result":"success",
"Key":"52523",
"Data":[
{
"ID": "KDJBD34",
"name": "Name of event 3",
"date": "date of event 3",
"location": "location of event 3",
"lastChangedDate":"2015-12-03 05:25:11"
}
]
}
I want to construct an array containing all the events and their details, like this:
{
"Result": "success",
"Key": "12345",
"Data":[
{
"ID": "GFDCV34",
"name": "Name of event 1",
"date": "date of event 1",
"location": "location of event 1",
"lastChangedDate": "2015-12-03 11:14:27"
},
{
"ID": "IDJHE23",
"name": "Name of event 2",
"date": "date of event 2",
"location": "location of event 2",
"lastChangedDate": "2015-12-03 15:17:47"
},
{
"ID": "KDJBD34",
"name": "Name of event 3",
"date": "date of event 3",
"location": "location of event 3",
"lastChangedDate":"2015-12-03 05:25:11"
}
]
}
Anyone who can point me in the right direction?
You should operate through your first results and attach the new retrieved properties
var res = {
"Result": "success",
"Key": "12345",
"Data": [{
"ID": "GFDCV34",
"lastChangedDate": "2015-12-03 11:14:27"
}, {
"ID": "IDJHE23",
"lastChangedDate": "2015-12-03 15:17:47"
}, {
"ID": "KDJBD34",
"lastChangedDate": "2015-12-03 05:25:11"
}]
};
var tmp;
res.Data.map(function(val,i){
tmp = getdetails(val.ID);
Object.keys(tmp.Data[0]).map(function(v,j){
val[v] = tmp.Data[0][v];
});
});
Demo

Categories