/    Sign up×
Community /Pin to ProfileBookmark

Reading out json file

Hey there, so basically i’ve got a file on the server, which is in json format. It’s build like:

string:string:string:string
string:string:string:string
string:string:string:string

It’s about ~12.000 lines long. What would be the most efficient way to read that file out, and get every single one in a while loop? like:

var string 1 = array[0];
var string 2 = …

Thanks for any answers! ?

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMar 27.2017 — string:string:string:string

string:string:string:string

string:string:string:string[/QUOTE]
Don't understand this. Could you show the structure more detailed?

I recommend reading the file by PHP and transferring the structure to Javascript:
[CODE]<script>
var struct = <?php file_get_contents("your-json-file.json"); ?>;
// Presumed that the structure is an array:
for (var i = 0; i < struct.length; i++) {
// current element available as struct[i]
}
</script>[/CODE]


As this code uses PHP your file has to have the extension ".php".
Copy linkTweet thisAlerts:
@SempervivumMar 27.2017 — BTW: I do not recommend this:[CODE]var string 1 = array[0];
var string 2 = ...[/CODE]
but keeping the elements in an array instead as it's much easier to process them in a loop then.
Copy linkTweet thisAlerts:
@SempervivumMar 27.2017 — PS: Forgot "echo" and when the JSON contains linebreaks it has to be joined:[CODE]
<script>
var struct = <?php echo implode('', file("data.json")); ?>;
// Presumed that the structure is an array:
for (var i = 0; i < struct.length; i++) {
// current element available as struct[i]
}
</script>[/CODE]
Copy linkTweet thisAlerts:
@rootMar 27.2017 — string:string:string:string
string:string:string:string
string:string:string:string
is not JSON format...

JSON formatted string is {
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}


What you have is likely to be a Flat File (CSV type file) that is using a Colon as the field separator and each row is separated by a new line.
Copy linkTweet thisAlerts:
@rootMar 27.2017 — As for most efficent method of reading the file, I say do it server-side with the set of functions for CSV and Flat Files and convert the file in to something that is more accessible to javascript by turning it in to a JSON formatted string at the server, then load it in to the browser.
Copy linkTweet thisAlerts:
@shakazorbaMar 27.2017 — Hey there, so basically i've got a file on the server,

which is in json format. It's build like:

string:string:string:string

string:string:string:string

string:string:string:string
[/QUOTE]


No, you do not have a json

file built like that, maybe

it's built like this ...

{

"keya":"valuea",

"keyb":"valueb"

}


It's about ~12.000 lines long. What would be the most efficient way to read that file out,

and get every single one in a while loop? like:

var string 1 = array[0];

var string 2 = ...

Thanks for any answers! ?[/QUOTE]


<?php

$string = file_get_contents("yourFile.json");

$json_a = json_decode($string, true);

?>

now $json_a is an associatve array

of course this assumes you will

search the array with php

on the server

if you would like to work with

a JavaScript Object in the

browser it would be like this ...


[CODE]

<script>
var req=new XMLHttpRequest;
req.open('get','myJson.json',false);
req.send();
myObj=JSON.parse(req.responseText);
alert(myObj);
</script>

[/CODE]
×

Success!

Help @GoRoK spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...