JSON incomplete variable - php

for some reason I have a problem getting a JSON input into PHP. Basically, I am importing a variable from a url-encoded JSON, the bit of code I have problem with looks like this:
"nearest_area": [
{
"country": [
{"value": "Czech Republic"}
],
"region": [
{"value": "Moravskoslezsky Kraj" }
]
}
]
When I import and JSON_decode it in PHP, I used the exact same way of getting the two variables. For country I used
data->nearest_area[0]->country[0]->value;
and I got Czech Republic, for the other one I used region instead of country, but for some reason instead of Moravskoslezsky Kraj, I am always getting just the first word - "Moravskoslezsky".
The only reason I could think of what could be causing the problem is encoding. In fact, in Czech, the actual name of the region ("kraj") is "Moravskoslezský". I used the UTF8 decoding procedure and indeed I am getting the proper "ý" at the end instead of "y", but then it just skips the rest....
Any ideas what could be wrong?

OK, sorry I have figured it out, there was a problem with encoding the actual url.

Related

Convert Value in Large JSON File to CSV

First of all, I appreciate there are lots of answers regarding dealing with large JSON files. However, I have yet to find one that encounters my scenario.
The problem I face is that I have large JSON files (12mb) that look like this:
{
"range": "Sheet1!A1:P40571",
"majorDimension": "ROWS",
"values": [
[
"new_id",
"qty",
"total_job_cost",
"total_job_revenue",
"total_job_profit",
"total_job_margin"
],
[
"34244",
"5",
"211.25",
"297.00",
"85.75",
"28.87%"
],
[
"34244",
"10",
"211.25",
"297.00",
"85.75",
"28.87%"
],
...
]
}
And I wish to extract out the values array, and convert it into a csv that would like this:
new_id,total_job_cost,total_job_revenue,total_job_profit,total_job_margin
34244,211.25,297.00,85.75,28.87%
34245,211.25,297.00,85.75,28.87%
...
However, since the values array is so large, when I try to extract it using a PHP library for JSON parsing, my server crashes when it tries to read it.
Any suggestions or tips appreciated. Thanks.
You can't read json line by line,but not with any built in libraries. I wrote a simple Json parser for another answer here
Convert structure to PHP array
I had to make a slight modification to handle "real" json" In the switch change this token
case 'T_ENCAP_STRING':
if( $mode == 'key'){
$key .= trim($content,'"');
}else{
value .= unicode_decode($content); //encapsulated strings are always content
}
next($lexer_stream);//consume a token
break;
You can test it here
http://sandbox.onlinephpfunctions.com/code/b2917e4bb8ef847df97edbf0bb8f415a10d13c9f
and find the full (updated) code here
https://github.com/ArtisticPhoenix/MISC/blob/master/JasonDecoder.php
Can't guarantee it will work but it's worth a shot. It should be fairly easy to modify it to read your file.
If the problem is simply to convert the large JSON file to a CSV file, then perhaps a jq solution is admissible. Depending on the computing environment, jq can generally handle large files (GB) breezily, and with a little more effort, it can usually handle even larger files as it has a "streaming parser".
In any case, here is a jq solution to the problem as stated:
jq -r '(.values[] | [.[0,2,3,4,5]]) | #csv' data.json > extract.csv
For the sample input, this produces:
"new_id","total_job_cost","total_job_revenue","total_job_profit","total_job_margin"
"34244","211.25","297.00","85.75","28.87%"
"34244","211.25","297.00","85.75","28.87%"
This is valid CSV, and the use of #csv guarantees the result, if any, will be valid CSV, but if you want the quotation marks removed, there are several options, though whether they are "safe" or not will depend on the data. Here is an alternative jq solution that produces comma-separated values. It uses join(",") instead of #csv:
(.values[] | [.[0,2,3,4,5]]) | join(",")

Strange encoding issue in PHP

I have been banging my head in the wall for couple of hours, I still couldn't figure out the issue. I have an associative array and when I try to build a query with it, It shows weird characters in the browser.
$reportVars = [
"__report" => "alpha",
"start_date" => "2001",
"end_date" => "2002",
"dsp_id" => "SPP",
"current_sp_id" => "SPP_1",
"sp_name" => "fawzan"
];
print_r(http_build_query($reportVars));
This is the output I get in the browser
__report=alpha&start_date=2001&end_date=2002&dsp_id=SPP¤t_sp_id=SPP_1&sp_name=fawzan
Note the strange character (¤) in the output after SPP, Before you ask No, I did not copy it from anywhere. I just typed it with my bare hand.
Can anyone please help me here?
&curren is being converted to ¤
you may have few options now:
move the current_sp_id to top, making it the first variable so that there is no & before it
use &amp as separator instead of & only by using:
print_r(http_build_query($reportVars, '', '& amp;'));
(remove the space between & and amp, added it because it's being converted to & only here too).
P.S. php isn't causing this issue as per my understanding, it's how your browser treats &curren by probably converting it to ¤ itself

json_decode fails

I'm fairly new to json, and I'm having an issue with json_decode. I think I know why, but I haven't been able to sort out how to fix it.
Basically, I have a URL that supplies json info. I grab it using cURL, and return it as a PHP variable, and that's working just fine. I can print_r out all the info I want. However, when I use json_decode($json, true), it returns NULL.
I THINK it's because, technically, what's being returned is not a string, but more like an object - and I can't sort out how to grab the contents of that object.
For example, when I return the json stuff as a php variable:
print_r($json);
The output returned looks like so (I won't do it exactly, because it's HUGE, so I'll show you the layout to keep it simple)
MyThing.returnedItems({MyThing.returnedItems({
"projects":[{
"completed":"2010-12-21",
"status":"finished",
"favorited":0,
"started":"2010-12-20",
"percentage":78,
"permalink":"slug to post",
"size":"One size",
"thumbnail":{"src":"full path to full size image",
"medium":"full path to thumbnail"},
"name":"Some title here",
"notes":"description here",
"url":"URL to page",
"comments":0},
So you can see it's like a nested array. I don't mind that, but I'd like to be able to access all the key/value pairs of these arrays as PHP variables. But it seems because of the "MyThing.returnedItems()" surrounding it, it doesn't see it as a string to decode, so I get a NULL value every time.
Anyone know what I'm missing here? Once I figure out how to grab the stuff inside there, I think I've got it (simple foreach or whatnot to get the rest of the variables as needed), but I just can't seem to get in there.
This is valid JSON
{
"item1": [
{
"something": [],
"something else": "some value"
}
],
"another fun thing": [
{
"more fun": "fun value 1",
"even more!": "fun value 2"
}
],
"item2": {
"another thing": "another value"
}
}
This is not!
MyThing.returnedItems({
"item1":[{"something:[],
"something else": "some value"},
"another fun thing": [{"more fun": "fun value 1",
"even more!": "fun value 2"}]
],
"item2":{"another thing": "another value"}
})
Its a javascript method call
Okay, I just wanted to add that you all REALLY helped me out. Especially MaX, because by knowing the "official term" of what was happening, I had better searches and ended up finding some really interesting code that eventually landed me my solution. However, I did discover the reason why I was having my json wrapped in that weird function method call: the URL that the site gave me to access their API actually had that call in it, so it was returned wrapped in it. In other words, the json file URL I had was like so:
somesite.com/apicall.json?key=1234567890&callback=MyThing&version=0...
so as soon as I removed that "callback" section - BAM the json was no longer wrapped, and a whole new world of foreach functions opened up to me. So, even though the solution ended up being a REALLY stupid oversight on my part, I wanted to thank you all for your input, because I learned a whole lot of stuff I wasn't planning to today XD
Oh, and the code that actually ended up working (after I got rid of the callback part of the URL, because json_decode was still returning NULL on me) was this:
$data = json_decode(file_get_contents($json), true);
print_r($data); // array of everything available for me to mess with!
Thanks again for your help, everyone :) I REALLY appreciate it!

Quote in json data

Hi you can see there's quote before rural management, this is causing problem while json decoding.. how can i retrive this using PHP json_decode?
[{
"employ":{
"id":"234",
"name":""rural management"
},
"pos":{
"name":"Programme Officer"
}
}]
You can't. The data is not JSON.
You need to make sure the data is in JSON format before passing it to json_decode.
As a quick hack, you could just perform a string substitution and change "" to "\" globally, but that isn't a robust solution. There is something wrong with the underlying JSON generator that you are getting the data from and that needs to be fixed at the source (probably by switching to using a JSON library instead of smashing strings together).

Cakephp JSON response with json_encode not working

I'm using ajax function to get the data back for my jquery autocomplete but it seems to not parse the json response and I cannot find why.
I look did console.log for one that is working which is another json response and the other one which is not working and in chrome Console, I can see below. The first one is not working and second one is working.
["17","17","16","20","19","18","23","18","20","18","23","23"]
["25", "24", "25", "24", "24", "23", "21", "23", "22", "21", "22", "22"]
I can see that the second one has red color on numbers and I cannot find out why.
Can someone find out why it's not parsing this JSON?
The second one has a space (or some other character that displays as such) after each comma. If this is how your json_encode() call outputs it, you can run an additional measure to ensure those spaces aren't there before returning it to your AJAX function:
$encoded_text = str_replace(" ","",$encoded_text);
Don't use that if you may have data which has spaces normally. I'm not sure if that is what is causing it to not work, but that is the only difference in the strings you showed us.
You can also use JSONLint to validate your JSON for free.

Categories