I tried to view my JSON file as a tree and I got an error that I don't know how to solve.
error:
Parse error on line 1: ...2","Likes":"0/0"}} {"Id":2,"commContent ----------------------^ Expecting 'EOF', '}', ',', ']', got '{'
JSON output:{"Id":1,"commContent":{"Name":"username1","Comment":"text1","Date":"5.5.2021 10:22","Likes":"0/0"}} {"Id":2,"commContent":{"Name":"username2","Comment":"text2","Date":"5.5.2021 10:24","Likes":"0/0"}}
Json online editor Iam using: https://jsoneditoronline.org/#
There is a problem with the JSON you have produced. You have two distinct elements in the JSON but they are not in an array, so the tree looks like this:
Root element
Root element
The error messages is correctly identifying the problem - it is expecting EOF but instead is receiving a new element starting with {.
That doesn't make any sense - the elements probably need to be nested in an array (surrounded with [] and with a comma , between), like this:
[
{
"Id": 1,
"commContent": {
"Name": "username1",
"Comment": "text1",
"Date": "5.5.2021 10:22",
"Likes": "0/0"
}
},
{
"Id": 2,
"commContent": {
"Name": "username2",
"Comment": "text2",
"Date": "5.5.2021 10:24",
"Likes": "0/0"
}
}
]
The above would be considered valid.
Related
I am using the NuGet package Hl7.Fhir.STU3 to save me from having to create all the POCO classes.
The Json returned from the NHS FHIR Server is (I've removed a huge chunk to make it easier to read):
{
"id": "000049660991",
"meta": {
"versionId": "3",
"profile": [
"https://fhir.nhs.uk/STU3/StructureDefinition/eRS-ReferralRequest-1"
]
},
"resourceType": "ReferralRequest",
"supportingInfo": [],
"intent": "plan"
}
The problem comes when I try to deserialize it as the "supportingInfo" element contains an empty array so I receive the following error (ignore the position):
Hl7.Fhir.Serialization.DeserializationFailedException: 'One or more errors occurred. (An array needs to have at least one element. At line 1, position 2445.)'
Is there a way to ignore empty arrays?
I've filtered values in a json file that I obtained from Google Maps API with the following command:
jq '.[] | select(.vicinity|contains("Bern"))' Bern.json > Bern_test.json
(I only want to have places in Bern)
I tried loading the Bern_test.json file to Tableau to visualize it, but Tableau complains that the format is wrong. json validator online also says something is wrong:
Error: Parse error on line 38:
... Wohlen bei Bern"} { "business_status"
----------------------^
Expecting 'EOF', '}', ',', ']', got '{'
Here is the "broken" part (if it helps):
"reference": "ChIJ-abNilA5jkcRAh6Jj7dLx_c",
"scope": "GOOGLE",
"types": [
"restaurant",
"food",
"point_of_interest",
"establishment"
],
"vicinity": "Aumattweg 22, Wohlen bei Bern"
} {
"business_status": "OPERATIONAL",
"geometry": {
"location": {
"lat": 46.9684408,
"lng": 7.377661100000001
I don't know why it got broken, or how I can properly save a json file after I've done some magic with jq. Can anyone help?
Your current filter produces a stream of JSON objects rather than a single JSON value. To fix that, wrap your filter in [...]:
jq '[.[] | select(.vicinity|contains("Bern"))]' Bern.json > Bern_test.json
or use the predefined map function to achieve the same result.
jq 'map(select(.vicinity|contains("Bern")))' Bern.json > Bern_test.json
The Newline delimited JSON file parsing correctly for below JSON Structure.
{
"name": "Honeywell",
"BOM": "12 GENUINE MIX RESISTOR KIT 150PCS 30\n1/21/4W + MIX zelatin CAPACITOR KIT\n3",
"url": "http://fairchild.com",
"image": "http://fairchild.com/over.jpg",
"ThresholdRange": "2M",
"Peak": "8",
"date": "2003-04-01",
"DeadTime": "15M"
},
But As soon i am adding below line it is throwing Error.
{
"name": "VishayElectronics.com",
"BOM": "12 GENUINE MIX RESISTOR KIT 150PCS 30\n1/21/4W + MIX power CAPACITOR KIT\n3",
"url": "http://vishay.com",
"image": "http://vishay/to.jpg",
"ThresholdRange": "10M",
"Peak": "8",
"date": "2011-06-06",
"DeadTime": "6M"
}
i am validating using https://jsonlint.com/
Error: Parse error on line 10:
...adTime": "15M"}{ "name": "VishayEl
--------------------^
Expecting 'EOF', '}', ',', ']', got '{'
May someone please help me what changes i should make to resolve error.
Many Thanks.
You lost a "," between your first object and the second one.
I have file that is list of JSON objects. It looks like this :
[
{
"id": 748,
"location": {
"slug": "istanbul",
"parent": {
"id": 442,
"slug": "turkey"
}
},
"rank": 110
},
{
"id": 769,
"location": {
"slug": "dubai",
"parent": {
"id": 473,
"slug": "uae"
}
},
"rank": 24
}
]
I want to create a list of hotel parent names, so i write this code to do this, I read the JSON file and assigned it to a variable, that part is correct. But look at this code :
with open('hotels.json', 'r', encoding="utf8") as hotels_data:
hotels = json.load(hotels_data)
parents_list = []
for item in hotels:
if item["location"]["parent"]["slug"] not in parents_list:
parents_list.append(item["location"]["parent"])
when i run this code, i give this error :
if item["location"]["parent"]["slug"] not in parents_list:
TypeError: 'NoneType' object is not subscriptable
This code does not work, so I tried to print the JSON objects so I wrote this in the loop:
print(item["location"]["parent"]["slug"])
This code prints the values I want, but also give me the exact same error.
thank you for any help.
I tried running the code and it seems to be working fine with your dataset.
However, instead of opening the file to read the data, I just assigned hotels with your dataset, hotels = [...].
The result I got was this:
[{'id': 442, 'slug': 'turkey'}, {'id': 473, 'slug': 'uae'}]
What is your result if you print hotels, is it the same as you shown here?
If you actually have a lot more data in your dataset, then I can presume that some of the dictionaries don't contain item["location"]["parent"]["slug"]. If that is the case, you should skip those by checking if that element exists in each item first before reading off from the parents_list.
For example:
try:
item["location"]["parent"]["slug"]
except (KeyError, TypeError) as e:
pass
else:
if item["location"]["parent"]["slug"] not in parents_list:
parents_list.append(item["location"]["parent"])
I cannot replicate the same error as you. The only thing that I can think of is that the last item in each object in the JSON shouldn't have a comma after it. See if that fixes your error
Should be a no brainer, but I'm can't seem to access the elements returned from Newtonsoft's json deserializer.
Example json:
{
"ns0:Test": {
"xmlns:ns0": "http:/someurl",
"RecordCount": "6",
"Record": [{
"aaa": "1",
"bbb": "2",
},
{
"aaa": "1",
"bbb": "2",
}]
}
}
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(somestring);
Stripping out the json up to the Record text, i can access the data without issue.
i.e. result.Recordcount
If i leave the json as shown above, can someone enlighten me how to access Recordcount?
All inputs appreciated. Thanks!
For those JSON properties that have punctuation characters or spaces (such that they cannot be made into valid C# property names), you can use square bracket syntax to access them.
Try this:
int count = result["ns0:Test"].RecordCount;