Related
I have a column in my data base called extra_fields, it's of the jsonb type, I don't know what might be in there, since my user can send unlimited data, but I wanted to retrieve the keys and map this column. The problem is that I need it to be multidimensional (nested), is it possible in postgres?
Example of column value:
Row 1
{
"name": "John Doe",
"address":{
"city": "London",
"country": "UK"
},
"status": [{
"value": "Inactive",
"inserted_at": "2050-01-02 12:00:00"
},{
"value": "Active",
"inserted_at": "2050-01-01 12:00:00"
}]
}
Row 2
{
"name": "John Doe",
"phone": "+345 43456576"
"address":{
"city": "London",
"country": "UK"
},
"status": [{
"value": "Inactive",
"inserted_at": "2050-01-02 12:00:00"
},{
"value": "Active",
"inserted_at": "2050-01-01 12:00:00"
}]
}
And i need to return the keys like this:
[name, phone, address: [city, country], status: [value, inserted_at]]
Any suggestions on how I can do this?
My stacks are nestjs, typeorm and postgres.
const data = {
"games": [
{
"id": "828de9122149499183df39c6ae2dd3ab",
"developer_id": "885911",
"game_name": "Minecraft",
"first_release": "2011-18-11",
"website": "https://www.minecraft.net/en-us"
},
{
"id": "61ee6f196c58afc9c1f78831",
"developer_id": "810637",
"game_name": "Fortnite",
"first_release": "2017-21-07",
"website": "https://www.epicgames.com/fortnite/en-US/home"
},
],
"developers": [
{
"id": "885911",
"name": "Mojang Studios",
"country": "US",
"website": "http://www.mojang.com",
},
{
"id": "750245",
"name": "God of War",
"country": "SE",
"website": "https://sms.playstation.com",
},
] };
I have json data like this. I want to display data like if developer_id = 885911(from games array) then print id(from developers array) and if the both are same then I want to print the name.(Mojang studios) and so on like games website etc. How can I do that?
This sample code will show you the developer of each game, if it's found:
const data = {
"games": [
{
"id": "828de9122149499183df39c6ae2dd3ab",
"developer_id": "885911",
"game_name": "Minecraft",
"first_release": "2011-18-11",
"website": "https://www.minecraft.net/en-us"
},
{
"id": "61ee6f196c58afc9c1f78831",
"developer_id": "810637",
"game_name": "Fortnite",
"first_release": "2017-21-07",
"website": "https://www.epicgames.com/fortnite/en-US/home"
},
],
"developers": [
{
"id": "885911",
"name": "Mojang Studios",
"country": "US",
"website": "http://www.mojang.com",
},
{
"id": "750245",
"name": "God of War",
"country": "SE",
"website": "https://sms.playstation.com",
},
] };
const gameDevelopers = data.games.map(g => ({
game: g.game_name,
developer: data.developers.find(d => d.id === g.developer_id)?.name || "No matching developer found"
}));
console.log(gameDevelopers)
What did you exactly need? i don't understand . But you can get value Mojang Studios
console.log(data.developers[0].name)
If you need all developer id then you can use
console.log(data.developers.map(data=>{
console.log(data.id)
}))
If you need the id where name is Mojang Studios
console.log(data.developers.map(data=>{
if(data.name == "Mojang Studios"){
console.log(data.id)
}
}))
I'm currently trying to code an application with javascript. It pulls data from a database and the response I'm getting is something like that:
{
"values":[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
}
But I need to have the JSON like that:
[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
How can I delete the parent values object in my JSON?
SHORT ANSWER:
Just access the values property like a JavaScript object.
LONG ANSWER:
You didn't post the JavaScript code snippet so it's quite difficult to give you an appropriate answer.
Assuming you have the following code:
const jsonString = getDataFromTheDB()
const jsonObject = JSON.parse(jsonObject) // still has the "values" layer
const values = jsonObject.values // what you want, without the "values" layer
// BONUS: Just in case you want to convert the object back to a JSON string but without the "values" layer
const valuesJSON = JSON.stringify(values, undefined, 2)
Based on this post :
just do this (consider json the variable that contains your json):
var key = "values";
var results = json[key];
delete json[key];
json = results;
console.log(json) will output the following:
[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
But you dont even have to do the last 2 steps of the code snippet above, you could also just directly use results variable and have the same output by console.log(results).
You could take the object and create a new variable with just the array.
var vals =
{
"values":[
{
"name": "Munich",
"location": "Germany",
"native_lang": "German",
},
{
"name": "London",
"location": "England",
"native_lang": "English",
},
{
"name": "Rome",
"location": "Italy",
"native_lang": "Italian",
}
]
}
var arr = vals.values;
console.log(arr);
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 }
I have an XML file that I read in to a Javascript object. This is called jsonXML. I can see the object in its entirety in the console when I log it. It is a pretty complex object with a lot of layers and duplication.
I need to access some of these duplicate objects but I can't seem to drill down into the specifics that I need. The problem I'm having is the duplicate objects.
Here's the structure of the JS object:
ProfileGroup:
File: (there are 85 "File Objects" I need to access)
0: (I think in the tree this is the array index of the File)
Profile:
WayPt: (there are two "WayPt" objects for each file I need to access both)
0: (I think in the tree this is the array index of the WayPt)
distance
localCoords
WayPt:
1: (I think in the tree this is the array index of the WayPt)
distance
localCoords
I was using this just to try and access the objects:
console.log(jsonXML.ProfileGroup.File.Profile.WayPt);
I can get the array of files with console.log(jsonXML.ProfileGroup.File);
But I can't get individual "File" objects or anything else past that.
Any info on this issue would be helpful. Thanks!
UPDATE
Here is some stingified JSON for those who would like to see it:
"xmlns": "www.geophysical.com/DZX/1.02",
"GlobalProperties": {
"verticalUnit": "cm",
"horizontalUnit": "m",
"dielectric": "1.8000000",
"readOnly": "0",
"unitsPerMark": "100.0000000",
"unitsPerScan": "0.0200000"
},
"Macro": {
"state": "0",
"Process": [
{
"state": "0",
"BinaryData": "0&``!```$$`!C``H``````\"`#\n"
},
{
"state": "0",
"BinaryData": "L+``!```$)`!-`C,ST[\\`````````````A$$````````````````````````#\n"
},
{
"state": "0",
"BinaryData": "ME#`!```$C#`[`P````!!``\"X00``A$(`````````````````````````````\nM````````````````````````````````#$H``,A\"``#_`P``````````````\nM````````````````````````````````````````````````````````````\n/````````````````````\n"
},
{
"state": "0",
"BinaryData": "M2``!```$0``$```````#```````````````````````#`V0```#_`P``````\n;````````````````````````````````````\n"
},
{
"state": "0",
"BinaryData": "0$``!```$\"``G`0```````\"`#\n"
}
]
},
"ProfileGroup": {
"scanRange": "0,88815",
"Radan3D": {
"localMinCoords": "-0.5000000,0.0000000,0.0000000",
"localMaxCoords": "18.0000000,25.0000000,0.0000000",
"globalMinCoords": "0.0000000,0.0000000,0.0000000",
"globalMaxCoords": "0.0000000,0.0000000,0.0000000",
"localRotationAngle": "0.0000000",
"displayXDirProfs": "1",
"displayYDirProfs": "1",
"displayOtherDirProfs": "0",
"hide": "0",
"localGain": "0.0000000",
"displayOrder": "0",
"flipDataInVertDir": "0",
"flipDataInHorizDir": "0"
},
"File": [
{
"scanRange": "0,887",
"name": "HARMONY __050.DZT",
"Profile": {
"scanRange": "0,887",
"Comment": {
"scan": "0",
"description": "Data Collection Notes: "
},
"WayPt": [
{
"scan": "0",
"mark": "User",
"name": "Mark2",
"distance": "0.0000000",
"localCoords": "0.0000000,0.5000000,0.0000000"
},
{
"scan": "887",
"distance": "18",
"localCoords": "18,0.5000000,0.0000000"
}
]
}
},
{
"scanRange": "888,1785",
"name": "HARMONY __051.DZT",
"Profile": {
"scanRange": "888,1785",
"Comment": {
"scan": "888",
"description": "Data Collection Notes: "
},
"WayPt": [
{
"scan": "888",
"mark": "User",
"name": "Mark2",
"distance": "0.0000000",
"localCoords": "18.0000000,0.5208333,0.0000000"
},
{
"scan": "1785",
"distance": "17.9400000",
"localCoords": "0.0600000,0.5208333,0.0000000"
}
]
}
},
{
"scanRange": "1786,2681",
"name": "HARMONY __052.DZT",
"Profile": {
"scanRange": "1786,2681",
"Comment": {
"scan": "1786",
"description": "Data Collection Notes: "
},
"WayPt": [
{
"scan": "1786",
"mark": "User",
"name": "Mark2",
"distance": "0.0000000",
"localCoords": "-0.5000000,1.0416667,0.0000000"
},
{
"scan": "2681",
"distance": "17.9000000",
"localCoords": "17.4000000,1.0416667,0.0000000"
}
]
}
},
{
"scanRange": "2682,3574",
"name": "HARMONY __053.DZT",
"Profile": {
"scanRange": "2682,3574",
"Comment": {
"scan": "2682",
"description": "Data Collection Notes: "
},
"WayPt": [
{
"scan": "2682",
"mark": "User",
"name": "Mark2",
"distance": "0.0000000",
"localCoords": "18.0000000,1.5625000,0.0000000"
},
{
"scan": "3574",
"distance": "17.8400000",
"localCoords": "0.1600000,1.5625000,0.0000000"
}
]
}
},
{
"scanRange": "3575,4470",
"name": "HARMONY __054.DZT",
"Profile": {
"scanRange": "3575,4470",
"Comment": {
"scan": "3575",
"description": "Data Collection Notes: "
},
"WayPt": [
{
"scan": "3575",
"mark": "User",
"name": "Mark2",
"distance": "0.0000000",
"localCoords": "-0.5000000,2.0833333,0.0000000"
},
{
"scan": "4470",
"distance": "17.9000000",
"localCoords": "17.4000000,2.0833333,0.0000000"
}
]
}
},
{
"scanRange": "4471,5370",
"name": "HARMONY __055.DZT",
"Profile": {
"scanRange": "4471,5370",
"Comment": {
"scan": "4471",
"description": "Data Collection Notes: "
},
"WayPt": [
{
"scan": "4471",
"mark": "User",
"name": "Mark2",
"distance": "0.0000000",
"localCoords": "18.0000000,2.6041667,0.0000000"
},
{
"scan": "5370",
"distance": "17.9800000",
"localCoords": "0.0200000,2.6041667,0.0000000"
}
]
}
}
]
}
}
The file is very long so I truncated it at the end of a File.
I actually looked around and figured out how to do this. I found it under a different topic but figured that I might help anyone else with this specific question.
To get to these multiple array items, you have to put in the index like this:
console.log(jsonXML.ProfileGroup.File["0"].Profile.WayPt["0"].distance);
When iterating through the object, you can use this:
files=[]; //array of file names
daters=[]; //final array of arrays
tempVal =[]; //temporary array
subArray=[]; //temporary array of sub values
var filz = jsonXML.ProfileGroup.File.length;
//depending on how many files there are, find out how many
for (i=0;i<filz;i++)
{
//add each file name to an array called 'files'
files.push(jsonXML.ProfileGroup.File[i].name);
//since in my example there are two objects called WayPt
for (j=0; j<2; j++)
{
//I use an array here to make it easy to combine into the other array
tempVal.push(jsonXML.ProfileGroup.File[i].Profile.WayPt[j].distance);
//since I have a string of comma separated values, make an array of these
subArray = jsonXML.ProfileGroup.File[i].Profile.WayPt[j].localCoords.split(',');
//combine the two above arrays
arrayNew = tempVal.concat(subArray)
push this array into the final array to use later
daters.push(arrayNew);
//reset the temporary values otherwise they will keep getting larger
tempVal=[];
arrayNew=[];
subArray=[];
}
}
This is how you drill down into the object when there are multiple entries in a named field.
Hope this helps anyone else!