1

I'm working with a script to search for places in Google Maps using their API. I use my local PC (Windows 10) as my development environment. The part of the code that matters is below:

$api_call_url ="https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=$lat,$lon&radius=$rad&type=$place_type&key=$Api_Key"
$api_call_response=Invoke-WebRequest $api_call_url
$api_call_obj = ConvertFrom-Json -InputObject $api_call_response
Add-Content "$file_name_json" $api_call_response

The json returned by Google is stored in the $api_call_response variable and then appended to a text file using Add-Content. Once the script was completed and tested, I moved it to the production environment (Windows Server 2016 Std). Ran the script and found out that the way the json is written to the text file is different.

This is what is written to the text file from my Windows 10 PC (multiple lines for each result)

{
    "html_attributions" : [],
    "results" : [],
    "status" : "ZERO_RESULTS"
}

{

    "html_attributions" : [],
    "results" : [],
    "status" : "ZERO_RESULTS"
}

This is what is written to the text file from my Windows Server 2016 (One line each result)

 {   "html_attributions" : [],   "results" : [],   "status" : "ZERO_RESULTS"}
 {   "html_attributions" : [],   "results" : [],   "status" : "ZERO_RESULTS"}

If I write-output the variable $api_call_response to screen, it displays with multiple lines.

I need the output with multiple lines because there is another script that cleans the file from results that I do not need and since the resulting text file is lots of jsons appended, it creates only one json file from all jsons. But it expect the results file the way my development environment writes it. Obviously, I'm trying to avoid modifying the cleaning script.

PSVersion on Windows Server 2016: 5.1.14393.3053
PSVersion on Windows 10: 5.1.17763.771

Any Thoughts?

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
lemiranda
  • 13
  • 2
  • 1
    Forgive me if i misunderstand the question but the way the json is structured has nothing to do with how the data will be read. Whatever parser was used to print the json object just happens to print it on one single line instead of 2. Why? I don't necessarily know but it wont have any impact on how you read the data. – Kevin Hernandez Dec 16 '19 at 20:13
  • 1
    @KevinHernandez - Except in the case where someone is trying to manually manipulate strings as opposed to JSON objects and then converting to a standard output format. I suspect we're encountering that here, based on the note about *"another script that cleans the file from results"* – gravity Dec 16 '19 at 20:22
  • @gravity Ah, got it. I knew was afraid that I would misunderstand the question. In that case Id say have a middle man script that re parses the json file back to non-one-liners? – Kevin Hernandez Dec 16 '19 at 20:25
  • 1
    No, I think you got it Kevin. In fact, I think you've led the OP in the right direction - handle the individual objects, and avoid having different servers (encoding defaults maybe?) "writing out" output that is parsed and such. – gravity Dec 16 '19 at 20:27
  • 1
    Completely agree about parsing regardless how the json has multiple lines vertically or one line horizontal. My case, I have a text file with multiple json structures that I have to manipulate to convert them in only one big json structure before inserting it to the database. The script I wrote to manipulate the json structures expect them in the way they are written in my development environment. My last option is to re-write the script. – lemiranda Dec 17 '19 at 15:32
  • Well, when opened the file using WordPad, it is written the way I needed, So i guess this has to be the way notepad displays the file. The scripts that manipulates the data worked fine. – lemiranda Dec 18 '19 at 20:18

2 Answers2

0

Well, when opened the file using WordPad, it is written the way I needed, So i guess this has to be the way notepad displays the file.

lemiranda
  • 13
  • 2
  • That sounds like "unix text", with line feeds `\`n` but no carriage returns `\`r`. – js2010 Dec 18 '19 at 20:38
  • Did you use Notepad on both machines to view the files, though? If so, that wouldn't really explain it, unless the different versions of Notepad on the two different OSes are why it's being displayed differently. It would make this answer more helpful if you could open both files on the same machine with the same application to compare the results, or even use an application that has better encoding/newline handling (such as Notepad++) or no text handling at all (such as a hex editor) to compare. As it is it would be good to provide a concrete explanation instead of premising it on "i guess". – Lance U. Matthews Dec 18 '19 at 20:39
  • Copied the file from the server to my local PC and opened it using notepad. Displays multiple lines. Copy a file created in my local PC to the server. Opened with notepad. Display json structures in one line. I do not have a "concrete explanation" for this. – lemiranda Dec 19 '19 at 21:08
0

If it's a matter of converting unix text (\n) to windows text (\r\n):

get-content file1 | set-content file2

Or with the same file:

(get-content file) | set-content file

Notepad can't display unix text correctly, but Wordpad can. (Although Notepad can handle unicode no bom.)

Other conversion methods: Unix newlines to windows newlines (on Windows)

js2010
  • 23,033
  • 6
  • 64
  • 66
  • This works! However the script I made to manipulate the file worked OK since looks like it does not care if it is UNIX or Windows txt. But have the answer now in case it happens to me again in a future script development. Thanks a lot. – lemiranda Dec 19 '19 at 21:13