I am getting unexpected token error when trying to parse this JSON string
$scope.feeds = JSON.parse('[{"id":"212216417436_10152811286407437","from":{ "category":"Movie","name":"The Lord of the Rings Trilogy","id":"212216417436"},"story":"The Lord of the Rings Trilogy shared The Hobbit\'s photo.","picture":"https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-9/s130x130/1912397_736719376363670_7288336626443028501_n.jpg?oh=a7e83093f61fafa7909ff84263f989e4&oe=54B4481D&__gda__=1420472696_dbd6462629a00e218c849c5ed7b49c44","link":"https://www.facebook.com/TheHobbitMovie/photos/a.291674244201521.63650.160617097307237/736719376363670/?type=1","name":"Timeline Photos","caption":"\"I have the only right.\" \n\nWhat is your favorite Thorin quote from #TheHobbit?","properties":[ { "name":"By","text":"The Hobbit","href":"https://www.facebook.com/TheHobbitMovie?ref=stream"}],"icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif","actions":[ { "name":"Comment","link":"https://www.facebook.com/212216417436/posts/10152811286407437"},{ "name":"Like","link":"https://www.facebook.com/212216417436/posts/10152811286407437"}],"privacy":{ "value":""},"type":"photo","status_type":"shared_story","object_id":"736719376363670","application":{ "name":"Photos","id":"2305272732"},"created_time":"2014-10-12T21:35:41+0000","updated_time":"2014-10-12T21:37:17+0000"},{ "id":"21785951839_10152794856921840","from":{ "category":"App page","name":"9GAG","id":"21785951839"},"message":"Must NOT let my girlfriend see this!\nhttp://9gag.tv/p/a9OrlY?ref=fbl9\n\nWatch more awesome videos on your phone:\nAndroid ► http://goo.gl/Rbi9FP\niPhone/iPad ► http://goo.gl/NFMm9p","picture":"https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCee84KqIBIdnIX&w=158&h=158&url=http%3A%2F%2Fd1d4324mp0stc8.cloudfront.net%2Fmedia%2Fphoto%2FpPgXmO5QJ_600w_v1.jpg","link":"http://9gag.tv/p/a9OrlY?ref=fbl9","name":"This \"Marry Me\" Lip-Sync Proposal Is So Touching And Amazing | 9GAG.tv","caption":"9gag.tv","description":"This sets the bar pretty high!","icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif","actions":[ { "name":"Comment","link":"https://www.facebook.com/21785951839/posts/10152794856921840"},{ "name":"Like","link":"https://www.facebook.com/21785951839/posts/10152794856921840"}],"privacy":{ "value":""},"type":"link","status_type":"shared_story","created_time":"2014-10-12T21:15:00+0000","updated_time":"2014-10-12T21:15:46+0000","shares":{ "count":160}}]');
Expanded string object:
{
"id":"212216417436_10152811286407437",
"from":{
"category":"Movie",
"name":"The Lord of the Rings Trilogy",
"id":"212216417436"
},
"story":"The Lord of the Rings Trilogy shared The Hobbit\'s photo.",
"picture":"https://fbcdn-sphotos-c-a.akamaihd.net/hphotos-ak-xaf1/v/t1.0-9/s130x130/1912397_736719376363670_7288336626443028501_n.jpg?oh=a7e83093f61fafa7909ff84263f989e4&oe=54B4481D&__gda__=1420472696_dbd6462629a00e218c849c5ed7b49c44",
"link":"https://www.facebook.com/TheHobbitMovie/photos/a.291674244201521.63650.160617097307237/736719376363670/?type=1",
"name":"Timeline Photos",
"caption":"\"I have the only right.\" \n\nWhat is your favorite Thorin quote from #TheHobbit?",
"properties":[
{
"name":"By",
"text":"The Hobbit",
"href":"https://www.facebook.com/TheHobbitMovie?ref=stream"
}
],
"icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif",
"actions":[
{
"name":"Comment",
"link":"https://www.facebook.com/212216417436/posts/10152811286407437"
},
{
"name":"Like",
"link":"https://www.facebook.com/212216417436/posts/10152811286407437"
}
],
"privacy":{
"value":""
},
"type":"photo",
"status_type":"shared_story",
"object_id":"736719376363670",
"application":{
"name":"Photos",
"id":"2305272732"
},
"created_time":"2014-10-12T21:35:41+0000",
"updated_time":"2014-10-12T21:37:17+0000"
},
{
"id":"21785951839_10152794856921840",
"from":{
"category":"App page",
"name":"9GAG",
"id":"21785951839"
},
"message":"Must NOT let my girlfriend see this!\nhttp://9gag.tv/p/a9OrlY?ref=fbl9\n\nWatch more awesome videos on your phone:\nAndroid ► http://goo.gl/Rbi9FP\niPhone/iPad ► http://goo.gl/NFMm9p",
"picture":"https://fbexternal-a.akamaihd.net/safe_image.php?d=AQCee84KqIBIdnIX&w=158&h=158&url=http%3A%2F%2Fd1d4324mp0stc8.cloudfront.net%2Fmedia%2Fphoto%2FpPgXmO5QJ_600w_v1.jpg",
"link":"http://9gag.tv/p/a9OrlY?ref=fbl9",
"name":"This \"Marry Me\" Lip-Sync Proposal Is So Touching And Amazing | 9GAG.tv",
"caption":"9gag.tv",
"description":"This sets the bar pretty high!",
"icon":"https://fbstatic-a.akamaihd.net/rsrc.php/v2/yD/r/aS8ecmYRys0.gif",
"actions":[
{
"name":"Comment",
"link":"https://www.facebook.com/21785951839/posts/10152794856921840"
},
{
"name":"Like",
"link":"https://www.facebook.com/21785951839/posts/10152794856921840"
}
],
"privacy":{
"value":""
},
"type":"link",
"status_type":"shared_story",
"created_time":"2014-10-12T21:15:00+0000",
"updated_time":"2014-10-12T21:15:46+0000",
"shares":{
"count":160
}
}
verify your json string. If it is ok try this:
var jsonStr="your json string";
var json=JSON.stringify(jsonStr);
json=JSON.parse(json)
When you're typing a string in javascript, the \ is treated as escaping character, while you need it in your json to actually be a \ and not escaping character.
You need to change the \" to \\" and \n to \\n, while keeping the \' because it is already an escaping character and it should be parsed okay
Related
So i have a json:
{
"code": "Q0934X",
"name": "PIDBA",
"longlat": "POINT(23.0 33.0)",
"altitude": 33
}
And i want to change the column code to Identifier
The wished output is this
{
"Identifier": "Q0934X",
"name": "PIDBA",
"longlat": "POINT(23.0 33.0)",
"altitude": 33
}
How can i do in the shortest way? Thanks
It appears that both "the json" you have and your desired result are JSON strings. If the one you have is json_str you can write:
json = JSON.parse(json_str).tap { |h| h["Identifier"] = h.delete("code") }.to_json
puts json
#=> {"name":"PIDBA","longlat":"POINT(23.0 33.0)","altitude":33,"Identifier":"Q0934X"}
Note that Hash#delete returns the value of the key being removed.
Perhaps transform_keys is an option.
The following seems to work for me (ruby 2.6):
json = JSON.parse(json_str).transform_keys { |k| k === 'code' ? 'Identifier' : k }.to_json
But this may work for Ruby 3.0 onwards (if I've understood the docs):
json = JSON.parse(json_str).transform_keys({ 'code': 'Identifier' }).to_json
I have quite a complicated structure of lists with lists inside, which i encode to JSON in order to save the game, and then decode it back.
I was certain all works fine, but then suddenly, one of the zeros is encoded as -1.#IND00
During the particular test I found it out, there were five lists of the same class as this, and only the final one ended up encoded in a wrong way.
code:
for(var zG=0; zG<ds_list_size(global.wojny); zG++){
var j_map=ds_map_create();
ds_map_add_list(j_map, "provinces", global.wojny[|zG]);
global.wojny[|zG] = json_encode(j_map);
ds_map_destroy(j_map);
}
full encoded list:
8th element saves as -1.#IND00
"{ "provinces": [ 9.000000, 0.000000, 9.000000, "{ \"countries1\": [ ] }",
"{ \"countries2\": [ ] }", 0.000000, 0.000000, 9991.000000, -1.#IND00,
100.000000, 13983.000000, 13984.000000, 0.000000, 0.000000 ] }"
a similar list, saved properly in the same for using the same code
"{ "provinces": [ 5.000000, 8.000000, 3.000000, "{ \"countries1\": [ ] }",
"{ \"countries2\": [ ] }", 169.000000, 184.000000, 8614.000000, 128.000000,
66.666667, 8626.000000, 8627.000000, 0.000000, 0.000000 ] }"
a list that is encoded properly
I think I know how to hot fix it - that particular value was a result of mathematical calculations that took place throughout the game, it started as 100 and got reduced to 0 by subtracting real values from it.
My hot fix was to include this into the for before encoding it to json:
var zGwojnapom = global.wojny[|zG];
zGwojnapom[|8] = round(real(zGwojnapom[|8]));
I have the following JSON file:
"spells": [
{
"spell":"Aberto",
"effect":"opens objects",
"_id":"5b74ebd5fb6fc0739646754c",
"type":"Charm"
},
{
"spell":"Accio",
"effect":"Summons an object",
"__v":0,
"_id":"5b74ecfa3228320021ab622b",
"type":"Charm"
},
{
"spell":"Age Line",
"effect":"Hides things from younger people",
"__v":0,
"_id":"5b74ed2f3228320021ab622c",
"type":"Enchantment"
},
{
"spell":"Aguamenti",
"effect":"shoots water from wand",
"__v":0,
"_id":"5b74ed453228320021ab622d",
"type":"Charm"
},
{
"spell":"Alarte Ascendare",
"effect":"shoots things high in the air",
"__v":0,
"_id":"5b74ed583228320021ab622e",
"type":"Spell"
}
}
Can you help me how to count all the spells with XQuery where the "type" = "Spell" and separately all the spells where the "Type"= "charm". The JSON file is much bigger, I just don't wanted to paste here the whole file. Thank you.
It seems like a straight-forward grouping and counting then:
declare variable $spell-types as xs:string* external := ('Spell', 'Charm');
for $spell in ?spells?*[?type = $spell-types]
group by $t := $spell?type
return $t || ' : ' || count($spell)
https://xqueryfiddle.liberty-development.net/nc4P6y2
Or, as Michael Kay has pointed out, with a given sequence of values it suffices to use
for $spell-type in $spell-types
return $spell-type || ' : ' || count(?spells?*[?type = $spell-type])
https://xqueryfiddle.liberty-development.net/nc4P6y2/1
Trying to figure out why the top .json file returns an error while the bottom works fine. (sorry if format is terrible, first post here) working code is from GitHub so i don't have very much knowledge of the 'inner workings'(mac OS). TIA
{
"regions": [
"cville",
"tech",
"downtown",
"outskirts",
"allregions"
],
"region_subregions": {
"cville": ["tech", "downtown"],
"allregions": ["cville", "tech", "downtown", "northcv", "southcv", "westcv", "canecreek"]
},
"region_neighborhoods": {
"southcv": ["parkview", "sink", "fairground"],
}
}
{
"regions": [
"sf",
"eastsf",
"centralsf",
"westsf",
"southsf",
"peninsula",
"sanjose",
"marin",
"eastbay",
"sacramento",
"allregions"
],
"region_subregions": {
"sf": ["eastsf", "centralsf", "westsf"],
"allregions": ["eastsf", "centralsf", "westsf", "southsf", "peninsula", "sanjose", "marin", "eastbay", "sacramento"]
},
"region_neighborhoods": {
"eastsf": ["embarcadero-p39-northbeach", "missionbay-dogpatch", "soma-ferry_bldg", "fidi-union_sq", "tenderloin-civic_center", "nob-russian_hill"],
"centralsf": ["haight-castro", "mission-dolores", "fillmore-hayes_valley"],
"westsf": ["presidio-marina", "richmond-gg_park", "sunset-", "lakeview-lake_merced"],
"southsf": ["glen_park-twin_peaks", "ingleside-", "excelsior-portola", "outer_mission-cow_palace", "bayview-", "south_city-brisbane", "daly_city-pacifica"],
"peninsula": ["half_moon_bay-coast", "san_bruno-burlingame", "san_mateo-foster_city", "belmont-sc-rws", "redwood_city-palo_alto", "mt-view_sunnyvale"],
"sanjose": ["san-jose"],
"marin": ["marin-"],
"eastbay": ["oakland-", "berkeley-", "east-bay"],
"sacramento": ["sacramento-"]
}
}
It's due to the trailing comma on this line
"southcv": ["parkview", "sink", "fairground"],
i have a grb2 file, i use grib2json covert it to .json, here is the json code:
[
{
"header":{
"discipline":0,
"disciplineName":"Meteorological products",
"gribEdition":2,
"gribLength":455039,
"center":7,
"centerName":"US National Weather Service - NCEP(WMC)",
"subcenter":0,
"refTime":"2016-10-05T12:00:00.000Z",
"significanceOfRT":1,
"significanceOfRTName":"Start of forecast",
"productStatus":0,
"productStatusName":"Operational products",
"productType":1,
"productTypeName":"Forecast products",
"productDefinitionTemplate":0,
"productDefinitionTemplateName":"Analysis/forecast at horizontal level/layer at a point in time",
"parameterCategory":2,
"parameterCategoryName":"Momentum",
"parameterNumber":3,
"parameterNumberName":"V-component_of_wind",
"parameterUnit":"m.s-1",
"genProcessType":2,
"genProcessTypeName":"Forecast",
"forecastTime":0,
"surface1Type":103,
"surface1TypeName":"Specified height level above ground",
"surface1Value":10.0,
"surface2Type":255,
"surface2TypeName":"Missing",
"surface2Value":0.0,
"gridDefinitionTemplate":0,
"gridDefinitionTemplateName":"Latitude_Longitude",
"numberPoints":259920,
"shape":6,
"shapeName":"Earth spherical with radius of 6,371,229.0 m",
"gridUnits":"degrees",
"resolution":48,
"winds":"true",
"scanMode":0,
"nx":720,
"ny":361,
"basicAngle":0,
"subDivisions":0,
"lo1":0.0,
"la1":90.0,
"lo2":359.5,
"la2":-90.0,
"dx":0.5,
"dy":0.5
},
"data":[
3.92,
3.89,
......
-3.06,
-3.07
]
}
]
when i use Cesium1.26 to read the json file, it works, but i can't get the header and data, can anyone help me about this problem?