How to add loop data to json object in CI? - javascript

I am trying to merge loop data to json object ,every time I am getting the last result of loop only ,so I declared that variable as a array ,its coming fine and I got my result but the format is not coming properly ,there is extra square brackets ,which I dont want ,because while parsing in front end ,its creating problem.
Below i am providing my codes.
{
"status": "success",
"statusReason": "Favouritelist",
"result": {
"Favouritelist": [
[ //want to remove this square bracket
{
"branch_id": "1234",
"branch_name": "avis1",
"branch_image": "uploads/avis.png",
"branch_gps": "12.9250,77.5938",
"branch_address": "eredfdf",
"branch_phone": "2147483647",
}
],//want to remove this square bracket
[//want to remove this square bracket
{
"branch_id": "1234",
"branch_name": "avis1",
"branch_image": "uploads/avis.png",
"branch_gps": "12.9250,77.5938",
"branch_address": "eredfdf",
"branch_phone": "2147483647",
}
]//want to remove this square bracket
]
}
}
My CI code
foreach ($tempquery->result() as $res1){
$car_id = $res1->car_id;
if(empty($timestamp)){
$this->db->select("IFNULL(branch.branch_id,'') as branch_id,IFNULL(branch.name,'') as branch_name,IFNULL(branch.image,'') as branch_image,IFNULL(branch.gps,'') as branch_gps,IFNULL(branch.address,'')as branch_address,IFNULL(branch.phone,'')as branch_phone,(select IFNULL(avg(rating),0) from branch_rating where user_id='$user_id' and delete_status ='false' )as branch_rating,IFNULL(car_id,'') as car_id,IFNULL(cars.name,'') as car_name,IFNULL(cars.image,'') as car_image,IFNULL(model,'') as model,IFNULL(price,'')as price,IFNULL(year,'')as year,IFNULL(type,'')as type,IFNULL(`no_of_seats`,'')as `no_of_seats`,IFNULL(`gear_type`,'') as `gear_type`,IFNULL(color,'') as color,insurance,status,IFNULL(description,'') as description,(select IFNULL(avg(rating),0) from car_rating where user_id='$user_id' and delete_status ='false' )as car_rating,car_types.typename AS carType,cars.petrol_type,car_types.car_type_id,cars.delivery_charges,cars.services");
$this->db->from("branch");
$this->db->join("cars","branch.branch_id=cars.branch_id");
$this->db->join('car_types', 'cars.car_type_id = car_types.car_type_id');
$this->db->where("cars.car_id",$car_id);
$this->db->where("branch.delete_status" , "false");
$this->db->where("cars.delete_status" , "false");
$car_details= $this->db->get();
$cdetail_new[]=$car_details->result(); ///added [] to mearge dat
}
Response::code_200("Favouritelist", array("Favouritelist" => $cdetail_new));
Can any one suggest me ,where I am doing wrong..
Thank you in ADV

Looks like the source is packed with the array. The result() function here, perhaps gives it as an array?
$car_details->result();
If that's the case, you might want to unwrap it here. You can use this notation if you are using latest version of PHP:
$cdetail_new[] = $car_details->result()[0];
If not, try the older one:
$result = $car_details->result();
$cdetail_new[] = $result[0];
You can check if this is the issue or not, i.e., if the result() is giving output as an array with a single element by using:
$car_details = $this->db->get();
var_dump($car_details->result()); // Is this resulting as an array?
die();
$cdetail_new[] = $car_details->result(); ///added [] to mearge dat

Related

Function returning object instead of Array, unable to .Map

I'm parsing an order feed to identify duplicate items bought and group them with a quantity for upload. However, when I try to map the resulting array, it's showing [object Object], which makes me think something's converting the return into an object rather than an array.
The function is as follows:
function compressedOrder (original) {
var compressed = [];
// make a copy of the input array
// first loop goes over every element
for (var i = 0; i < original.length; i++) {
var myCount = 1;
var a = new Object();
// loop over every element in the copy and see if it's the same
for (var w = i+1; w < original.length; w++) {
if (original[w] && original[i]) {
if (original[i].sku == original[w].sku) {
// increase amount of times duplicate is found
myCount++;
delete original[w];
}
}
}
if (original[i]) {
a.sku = original[i].sku;
a.price = original[i].price;
a.qtty = myCount;
compressed.push(a);
}
}
return compressed;
}
And the JS code calling that function is:
contents: compressedOrder(item.lineItems).map(indiv => ({
"id": indiv.sku,
"price": indiv.price,
"quantity": indiv.qtty
}))
The result is:
contents: [ [Object], [Object], [Object], [Object] ]
When I JSON.stringify() the output, I can see that it's pulling the correct info from the function, but I can't figure out how to get the calling function to pull it as an array that can then be mapped rather than as an object.
The correct output, which sits within a much larger feed that gets uploaded, should look like this:
contents:
[{"id":"sku1","price":17.50,"quantity":2},{"id":"sku2","price":27.30,"quantity":3}]
{It's probably something dead simple and obvious, but I've been breaking my head over this (much larger) programme till 4am this morning, so my head's probably not in the right place}
Turns out the code was correct all along, but I was running into a limitation of the console itself. I was able to verify this by simply working with the hard-coded values, and then querying the nested array separately.
Thanks anyway for your help and input everyone.
contents: compressedOrder(item.lineItems).map(indiv => ({
"id": indiv.sku,
"price": indiv.price,
"quantity": indiv.qtty
}))
In the code above the compressedOrder fucntion returns an array of objects where each object has sku, price and qtty attribute.
Further you are using a map on this array and returning an object again which has attributes id, price and quantity.
What do you expect from this.
Not sure what exactly solution you need but I've read your question and the comments, It looks like you need array of arrays as response.
So If I've understood your requirement correctly and you could use lodash then following piece of code might help you:
const _ = require('lodash');
const resp = [{key1:"value1"}, {key2:"value2"}].map(t => _.pairs(t));
console.log(resp);
P.S. It is assumed that compressedOrder response looks like array of objects.

How can I reformat this simple JSON so it doesn't catch "Circular structure to JSON" exception?

Introduction
I'm learning JavaScript on my own and JSON its something along the path. I'm working on a JavaScript WebScraper and I want, for now, load my results in JSON format.
I know I can use data base, server-client stuff, etc to work with data. But I want to take this approach as learning JSON and how to parse/create/format it's my main goal for today.
Explaining variables
As you may have guessed the data stored in the fore mentioned variables comes from an html file. So an example of the content in:
users[] -> "Egypt"
GDP[] -> "<td> $2,971</td>"
Regions[] -> "<td> Egypt </td>"
Align[] -> "<td> Eastern Bloc </td>"
Code
let countries = [];
for(let i = 0; i < users.length; i++)
{
countries.push( {
'country' : [{
'name' : users[i],
'GDP' : GDP[i],
'Region' : regions[i],
'Align' : align[i]
}]})
};
let obj_data = JSON.stringify(countries, null, 2);
fs.writeFileSync('countryballs.json', obj_data);
Code explanation
I have previously loaded into arrays (users, GDP, regionsm align) those store the data (String format) I had extracted from a website.
My idea was to then "dump" it into an object with which the stringify() function format would format it into JSON.
I have tested it without the loop (static data just for testing) and it works.
Type of error
let obj_data = JSON.stringify(countries, null, 2);
^
TypeError: Converting circular structure to JSON
--> starting at object with constructor 'Node'
| property 'children' -> object with constructor 'Array'
| index 0 -> object with constructor 'Node'
--- property 'parent' closes the circle
What I want from this question
I want to know what makes this JSON format "Circular" and how to make this code work for my goals.
Notes
I am working with Node.js and Visual Studio Code
EDIT
This is further explanation for those who were interested and thought it was not a good question.
Test code that works
let countries;
console.log(users.length)
for(let i = 0; i < users.length; i++)
{
countries = {
country : [
{
"name" : 'CountryTest'
}
]
}
};
let obj_data = JSON.stringify(countries, null, 2);
fs.writeFileSync('countryballs.json', obj_data);
});
Notice in comparison to the previous code, right now I am inputing "manually" the name of the country object.
This way absolutely works as you can see below:
Now, if I change 'CountryTest' to into a users[i] where I store country names (Forget about why countries are tagged users, it is out of the scope of this question)
It shows me the previous circular error.
A "Partial Solution" for this was to add +"" which, as I said, partially solved the problem as now there is not "Circular Error"
Example:
for(let i = 0; i < users.length; i++)
{
countries = {
country : [
{
"name" : users[i]+''
}
]
}
};
Resulting in:
Another bug, which I do not know why is that only shows 1 country when there are 32 in the array users[]
This makes me think that the answers provided are not correct so far.
Desired JSON format
{
"countries": {
"country": [
{
"name": "",
"GDP" : "",
"Region" : "",
"Align" : ""
},
{
"name": "",
"GDP" : "",
"Region" : "",
"Align" : ""
},
{
"name": "",
"GDP" : "",
"Region" : "",
"Align" : ""
}
]}
}
Circular structure error occurs when you have a property of the object which is the object itself directly (a -> a) or indirectly (a -> b -> a).
To avoid the error message, tell JSON.stringify what to do when it encounters a circular reference. For example, if you have a person pointing to another person ("parent"), which may (or may not) point to the original person, do the following:
JSON.stringify( that.person, function( key, value) {
if( key == 'parent') { return value.id;}
else {return value;}
})
The second parameter to stringify is a filter function. Here it simply converts the referred object to its ID, but you are free to do whatever you like to break the circular reference.
You can test the above code with the following:
function Person( params) {
this.id = params['id'];
this.name = params['name'];
this.father = null;
this.fingers = [];
// etc.
}
var me = new Person({ id: 1, name: 'Luke'});
var him = new Person( { id:2, name: 'Darth Vader'});
me.father = him;
JSON.stringify(me); // so far so good
him.father = me; // time travel assumed :-)
JSON.stringify(me); // "TypeError: Converting circular structure to JSON"
// But this should do the job:
JSON.stringify(me, function( key, value) {
if(key == 'father') {
return value.id;
} else {
return value;
};
})
The answer is from StackOverflow question,
Stringify (convert to JSON) a JavaScript object with circular reference
From your output, it looks as though users is a list of DOM nodes. Rather than referring to these directly (where there are all sort of possible cyclical structures), if you just want their text, instead of using users directly, try something like
country : [
{
"name" : users[i].textContent // maybe also followed by `.trim()
}
]
Or you could do this up front to your whole list:
const usersText = [...users].map(node => node.textContent)
and then use usersText in place of users as you build your object.
If GDP, regions and align are also references to your HTML, then you might have to do the same with them.
EUREKA!
As some of you have mentioned above, let me tell you it is not a problem of circularity, at first..., in the JSON design. It is an error of the data itself.
When I scraped the data it came in html format i.e <td>whatever</td>, I did not care about that as I could simply take it away later. I was way too focused in having the JSON well formatted and learning.
As #VLAZ and #Scott Sauyezt mentioned above, it could be that some of the data, if it is not well formatted into string, it might be referring to itself somehow as so I started to work on that.
Lets have a look at this assumption...
To extract the data I used the cheerio.js which gives you a kind of jquery thing to parse html.
To extract the name of the country I used:
nullTest = ($('table').eq(2).find('tr').eq(i).find('td').find('a').last());
//"Partial solution" for the OutOfIndex nulls
if (nullTest != null)
{
users.push(nullTest);
}
(nullTest helps me avoid nulls, I will implement some RegEx when everything works to polish the code a bit)
This "query" would output me something like:
whatEverIsInHereIfThereIsAny
or else.
to get rid off this html thing just add .html() at the end of the "jquery" such as:
($('table').eq(2).find('tr').eq(i).find('td').find('a').last().html());
That way you are now working with String and avoiding any error and thus solves this question.

set array as value in json format in Javascript

I want to add array as json value.
Json format is as follows.
json_data = [
'name':'Testing'
'email':'TestEmail'
'links':[
'test#test.com',
'test#test1.com',
'test#test3.com']
]
How can I set value of 'links' in javascript like that?
I did as follows.
links_array = [];
links_array =['testing','test2'];
json_data.links = links_array;
I wanted to append these two string but couldn't.
Any help would be appreciate.
Assuming that the syntax of your example is correct, you can use the "push" method for arrays.
json_data = {
'name':'Testing',
'email':'TestEmail',
'links':[]
};
json_data.links.push("test1#test.com");
json_data.links.push("test2#test.com");
json_data.links.push("test3#test.com");
You have to make little changes to make it work.
First thing, You have to replace initial square brackets with curly one. By doing this your object will become JSON Literal - a key value pair.
Second thing, You have missed commas after 'name':'Testing' and 'email':'TestEmail'
Below will work perfectly:
var json_data = {
'name':'Testing',
'email':'TestEmail',
'links':[
'test#test.com',
'test#test1.com',
'test#test3.com']
}
In addition to push as mentioned by #giovannilobitos you can use concat and do it all in one go.
var json_data = {
'name':'Testing',
'email':'TestEmail',
'links':[
'test#test.com',
'test#test1.com',
'test#test3.com'
]
};
var links_array = ['testing','test2'];
json_data.links = json_data.links.concat(links_array);
console.log(json_data.links);
On MDN's array reference you can find a more complete list of how to modify arrays in JavaScript.

How to properly loop through JSON response

Im trying to populate a combobox with data from database.
When I access mystream.php?theLocation=NewYork
I get this JSON response
RESULT
{"result":
[{"theID":"36"},{"theStream":"0817-05131"},{"theLabel":"hgjbn"},{"theLocation":"NewYork"},
{"theID":"37"},{"theStream":"0817-05131"},{"theLabel":"hgjbn"},{"theLocation":"NewYork"},
{"theID":"40"},{"theStream":"0817-31334"},{"theLabel":"dsfg ghjg"},{"theLocation":"NewYork"}]}
Applying the answer from this post
loop through JSON result with jQuery
I came up with this JSON
$.getJSON(
'mystream.php',
'theLocation=NewYork',
function(result){
$('#cmbNewYork').empty();
$.each(result, function(i, item){
$('#cmbNewYork').append('<option value=' +item.theStream+ '>'+item.theLabel+'</option>');
alert(item.theStream);
});
}
);
My resulting combo box only contains undefined.
How to properly loop thru JSON response?
Thanks
EDIT (ADDED)
mystream.php
$sql = "SELECT * FROM Streams WHERE theLocation='$loc'";
$res = mysqli_query($conn,$sql);
$result = array();
while($row = mysqli_fetch_array($res)){
array_push($result,
array('theID'=>$row['theID']),
array('theStream'=>$row['theStream']),
array('theLabel'=>$row['theLabel']),
array('theLocation'=>$row['theLocation'])
);
}
echo json_encode(array('result'=>$result));
Two issues:
The primary issue that your JSON format is very strange: It's an array of objects each of which has one name/value pair:
{"result": [
{"theID":"36"},
{"theStream":"0817-05131"},
{"theLabel":"hgjbn"},
{"theLocation":"NewYork"},
{"theID":"37"},
{"theStream":"0817-05131"},
{"theLabel":"hgjbn"},
{"theLocation":"NewYork"},
{"theID":"40"},
{"theStream":"0817-31334"},
{"theLabel":"dsfg ghjg"},
{"theLocation":"NewYork"}
]}
That's 12 separate objects.
You should have objects with all of those properties together:
{
"result": [
{
"theID": "36",
"theStream": "0817-05131",
"theLabel": "hgjbn",
"theLocation": "NewYork"
},
{
"theID": "37",
"theStream": "0817-05131",
"theLabel": "hgjbn",
"theLocation": "NewYork"
},
{
"theID": "40",
"theStream": "0817-31334",
"theLabel": "dsfg ghjg",
"theLocation": "NewYork"
}
]
}
That's three objects, each with four properties.
Re your edit to the question, you can do that like this:
while($row = mysqli_fetch_array($res)){
array_push($result,
array(
'theID'=>$row['theID'],
'theStream'=>$row['theStream'],
'theLabel'=>$row['theLabel'],
'theLocation'=>$row['theLocation']
)
);
}
Note how that's creating one array per loop, rather than four.
The second issue is that you probably need result.result, rather than just result, on this line:
$.each(result.result, function(i, item){
// ----------^^^^^^^
...since result is your overall anonymous result, which has a single property, result, which has your array.
If you fix those, your loop should start working.
You don't have to do the result.result thing if you don't want to. Instead, you could have your JSON define an array instead of an object with a single property referring to the array:
[
{
"theID": "36",
"theStream": "0817-05131",
"theLabel": "hgjbn",
"theLocation": "NewYork"
},
(and so on)
]
You haven't shown the PHP code creating $result, so I can't show you how to do that, but A) You don't need to, the result.result thing is fine, and B) If you want to, I'm sure you can figure it out.

Get comma separated string from array

I have the following JSON in JS.
{
"url":"http://example.com/main.aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"rokumar#example.com",
"alerts":["num_domains", "med_load_time", "avg_load_time", "tot_req"]
}
I want the list of alerts to be removed and replaced with comma separated values as follows.
{
"url":"http://example.com/main.aspx",
"report_template_id":"1",
"interval_secs":"86400",
"analysis_type":"lite",
"email":"rokumar#example.com",
"alerts":"num_domains,med_load_time,avg_load_time,tot_req"
}
Just adding all the steps:-
1). Taking your JSON in a variable.
data = {"url":"http://example.com/main.aspx","report_template_id":"1","interval_secs":"86400","analysis_type":"lite","email":"rokumar#example.com","alerts":["num_domains","med_load_time","avg_load_time","tot_req"]};
2). Parse JSON data to object. Assuming the JSON is a string initially do a typeof(data) to be clear.
data = JSON.parse(data);
3) Change list of alerts to comma separated values
data.alerts = data.alerts.join(',');
4) Convert back to string
data = JSON.stringify(data)
So data will look like
{
"url": "http://example.com/main.aspx",
"report_template_id": "1",
"interval_secs": "86400",
"analysis_type": "lite",
"email": "rokumar#example.com",
"alerts": "num_domains,med_load_time,avg_load_time,tot_req"
}
Note:- If you will just say join() then also it will work, because default delimiter is , only, just to clarify I have given that.

Categories

Resources