Angular 6 - Unexpected token in JSON at position 0 - json

My Angular 5 project was working without issues, just after having updated it to version 6, it stopped building using ng build due to the next:
ERROR in ./src/app/assets/i18/en.json Module parse failed: Unexpected
token in JSON at position 0 You may need an appropriate loader to
handle this file type.
here is my json file:
{
"app": {
"Welcome": "Welcome",
"New": "New"
},
"mainMenu": {
"Home": "Home",
"Logout": "Logout"
},
"pageHeader": {
"About": "About",
"Settings": "Settings"
}
}
Most solutions on the web are talking about CopyWebpackPlugin but the project doesn't use any Webpack configuration file.
then, following this link I tried to make the json as an array:
{
"menu":[
"app": {
"Welcome": "Welcome",
"New": "New"
},
"mainMenu": {
"Home": "Home",
"Logout": "Logout"
},
"pageHeader": {
"About": "About",
"Settings": "Settings"
}
]
}
But got the following error, despite the file contains 16 lines.
Unexpected token : in JSON at position 24
Any idea ?

Inspired by #AndrewJuniorHoward, found that while upgrade process, all the json files were encoded to UTF-8-BOM instead of UTF-8, that's why Angular was unable to load them during build.
In Visual Studio code, I just created empty files, pasted in them the content of the old json files and then overwritten them, and all worked perfectly.

Resave the angular.json file as UTF8. There seems to be a recent problem with upgrading to Angular 6 regarding this.

Hope you have resolved the issue but still if you want some minor changes you can try adding "id" to objects in array as below, I Tried this in my CLI project on Angular 6 while performing CURD operation in JSON file.
{
"menu":[
"app": {
"id": 1,
"Welcome": "Welcome",
"New": "New"
},
"mainMenu": {
"id": 2,
"Home": "Home",
"Logout": "Logout"
},
"pageHeader": {
"id": 3,
"About": "About",
"Settings": "Settings"
}
]
}

Related

How to define a variable within a JSON file and use it within JSON file

I'm trying to find if JSON file supports defining variables and using them within that JSON file?
{
"artifactory_repo": "toplevel_virtual_NonSnapshot",
"definedVariable1": "INSTANCE1",
"passedVariable2": "${passedFromOutside}",
"products": [
{ "name": "product_${definedVariable1}_common",
"version": "1.1.0"
},
{ "name": "product_{{passedVariable2}}_common",
"version": 1.5.1
}
]
}
I know YAML files allow this but now sure if JSON file allows this behavior or not. My plan is that a user will pass "definedVariable" value from Jenkins and I'll create a target JSON file (after substi
This might help you:
{
"artifactory_repo": "toplevel_virtual_NonSnapshot",
"definedVariable1": "INSTANCE1",
"passedVariable2": `${passedFromOutside}`,
"products": [
{ "name": `product_${definedVariable1}_common`,
"version": "1.1.0"
},
{ "name": `product_${passedVariable2}_common`,
"version": 1.5.1
}
]
}
*Note the use of `` instead of ''

OrientDB ETL Throws exception on loading CSV file

I am trying to load a simple CSV file to OrientDB and it always throws this exception.
CSV File Content
id, name, role
1, Sarath, Architect
2, Anoop, Project Manager
3, Nazeem, Lead Developer
4, Rini, Senior Developer
5, Shine, iOS Developer
6, Vishnu, iOS Developer
json config file
{
"source": { "file": { "path": "./dev.csv" } },
"extractor": { "row": {} },
"transformers": [
{
"csv": {}
},
{ "vertex": { "class": "Person" } }
],
"loader": {
"orientdb": {
"dbURL": "remote:localhost/dev",
"dbType": "graph",
"dbUser": "root",
"dbPassword": "root",
"dbAutoCreate": true,
"classes": [
{"name": "Person", "extends": "V"}
], "indexes": [
{"class":"Person", "fields":["id:integer"], "type":"UNIQUE" }
]
}
}
}
All the time it shows this exception. I tried different CSV options but it did not work. Looks like the file reading itself throwing exception.
ยป oetl posts.json sarat#Saraths-MacBook-Air
OrientDB etl v.2.0.1 (build #BUILD#) www.orientechnologies.com
Exception in thread "main" com.orientechnologies.orient.core.exception.OConfigurationException: Error on loading config file: posts.json
at com.orientechnologies.orient.etl.OETLProcessor.main(OETLProcessor.java:151)
Platform Details
OS X 10.10.2
java version "1.8.0_25"
OrientDB - v.2.0.1
The stack trace gives you the cause of the problem (you only showed the first, not interesting lines in your example).
Check:
The server is running (you specified remote:localhost/dev, so the server must be running on the default port at the same machine).
The database exists and has "root" / "root" as credentials (note that mostly "admin" "admin" is used)
The file dev.csv is present in the current directory
Tried out your example and works without modification.
I had the same problem, and it was due to one of the directories in the path to the JSON config file having a space in it. The oetl script doesn't handle this well and thus can't find the script.
#rmuller: The OP showed the entire traceback. That's all you get in this case.

Extracting data from a JSON file

I have a large JSON file that looks similar to the code below. Is there anyway I can iterate through each object, look for the field "element_type" (it is not present in all objects in the file if that matters) and extract or write each object with the same element type to a file? For example each user would end up in a file called user.json and each book in a file called book.json?
I thought about using javascript but to my knowledge js can't write to files, I also tried to do it using linux command line tools by removing all new lines, then inserting a new line after each "}," and then iterating through each line to find the element type and write it to a file. This worked for most of the data; however, where there were objects like the "problem_type" below, it inserted a new line in the middle of the data due to the nested json in the "times" element. I've run out of ideas at this point.
{
"data": [
{
"element_type": "user",
"first": "John",
"last": "Doe"
},
{
"element_type": "user",
"first": "Lucy",
"last": "Ball"
},
{
"element_type": "book",
"name": "someBook",
"barcode": "111111"
},
{
"element_type": "book",
"name": "bookTwo",
"barcode": "111111"
},
{
"element_type": "problem_type",
"name": "problem object",
"times": "[{\"start\": \"1230\", \"end\": \"1345\", \"day\": \"T\"}, {\"start\": \"1230\", \"end\": \"1345\", \"day\": \"R\"}]"
}
]
}
I would recommend Java for this purpose. It sounds like you're running on Linux so it should be a good fit.
You'll have no problems writing to files. And you can use a library like this - http://json-lib.sourceforge.net/ - to gain access to things like JSONArray and JSONObject. Which you can easily use to iterate through the data in your JSON request, and check what's in "element_type" and write to a file accordingly.

How to load JSON to D3 in Tree Diagram

I am a real dumb with HTML and JavaScript, so please excuse any dumbness.
I am using D3 Tree Diagram, but I need to load a JSON file instead of writing it inside the JS script, which the name of the file to be loaded will be chose by the user in a select tag. Here's the D3 code
First, how can I load/read a JSON file, lets say exampleNodes.json,
And then, how can I pass the name of the selected select tag so that it reads the appropriate JSON?
Thanks for your patience, and help. Thank you.
in code
var treeData = [
{
"name": "Top Level",
"parent": "null",
"children": [
{
"name": "Level 2: A",
"parent": "Top Level",
"children": [
{
"name": "Son of A",
"parent": "Level 2: A"
},
{
"name": "Daughter of A",
"parent": "Level 2: A"
}
]
},
{
"name": "Level 2: B",
"parent": "Top Level"
}
]
}
];
you have to save it on data.json file like
{
"treeData" : [ ... your data array ...]
}
after that in d3.json() function you will receive this object
d3.json("data.json",function(json){
// do your coding
// or all code put inside one function and call it after data loaded
});
if you are using google chrome than it will gave you error on data reading from json because security Google Chrome not allow read files from file system you can get data in Firefox. to make it run upload your code on some local server. i.e in WampServer or Apache tomcat etc.

Jquery JSON FORMAT ERROR

{"simpleSearchResult" :
{
"facultydetail":
[
{"comboValue":"FACULTY"},
{"facultyDetail":
[
"Android",
"Android profile",
"balaguru",
"Ulsoor",
"balaguru#savinirs.com",
"Bangalore",
"Karnataka",
"India",
"83",
"3 years of teaching experiance",
"INR",
"10000.0",
"40",
"20000.0"
]
},
{"NunberOfPages":"1"},
{"CurrentPage":"1"},
{"SkillIds":"an"},
{"cityId":"ba"},
{"DisplayCboValue":"STUDENT"},
{"Limit":"0"},
{"PageComboVal":"10"},
{"discipline":"Languages"}
]
}
}
This is my JSON string from my action class i am returning this to jQuery ajax call but that is always going to error block, gving parse error any one please help me to fix this issue
There are multiple JSON validators available online which can pinpoint the problem with JSON formatting. I used http://www.jsonlint.com/ and it appears that your JSON is valid. The problem is probably somewhere else.