JSON expected EOF, got ',' - json

I was converting my JSON file into .csv and while converting there was such mistake as "expected EOF, got ','"
{
"id": 22970,
"type": "message",
"date": "2018-11-24T21:08:21",
"edited": "1970-01-01T03:00:00",
"from": "lox",
"from_id": 731504644,
"text": "no"
},
{
"id": 22971,
"type": "message",
"date": "2018-11-24T21:08:32",
"edited": "1970-01-01T03:00:00",
"from": "Gox",
"from_id": 417024817,
"text": "ok"
}
It shows that the comma after first block must be deleted,but when i delete it next { glows red. How could i format this so it will be normally converted?

Your JSON is incorrect with or without a comma.
I believe you are trying to have an array of objects with JSON. If so, the syntax should look like this
[ // Array of objects
{ // Object 1
"id": 22970,
"type": "message",
"date": "2018-11-24T21:08:21",
"edited": "1970-01-01T03:00:00",
"from": "lox",
"from_id": 731504644,
"text": "no"
}, // Comma separating array items
{ // Object 2
"id": 22971,
"type": "message",
"date": "2018-11-24T21:08:32",
"edited": "1970-01-01T03:00:00",
"from": "Gox",
"from_id": 417024817,
"text": "ok"
}
]
I recommend using any online JSON parser to check for syntax erros in the future.

Related

How to a build a JSON file from a CSV using an existing JSON schema format?

I have a JSON schema and a CSV file. The CSV file has 2,511 rows and one header row (2,512 rows total). Each row has 43 columns. I was able to convert the CSV to a JSON using one of the myriad of online converters, but the result is what I believed is termed a 'flat JSON file'.
Here is the CSV header row:
F1,F2,F3.1.F1,F3.1.F2,F3.1.F3,F3.1.F4,...F3.10.F1,F3.10.F2,F3.10.F3,F3.10.F4,F4
Here is my JSON schema:
{
"$schema": "http://json-schema.org/schema#",
"$id": "./.schema.json",
"title": "",
"description": "",
"type": "object",
"properties": {
"F1": {
"description": "",
"type": "string"
},
"F2": {
"description": "",
"type": "string"
},
"F3": {
"description": "",
"type": "array",
"items": {
"description": "",
"type": "object",
"properties": {
"F3.F1": {
"description": "",
"type": "string"
},
"F3.F2": {
"description": "",
"type": "string"
},
"F3.F3": {
"description": "",
"type": "string"
},
"F3.F4": {
"description": "",
"type": "string"
}
},
"required": [
"F3.F1",
"F3.F2",
"F3.F3",
"F3.F4"
]
},
"numItems": 10,
"unique": false
},
"F4": {
"description": "",
"type": "string"
}
},
"required": [
"F1",
"F2",
"F3",
"F4"
],
"additionalProperties": false
}
From the CSV->JSON conversion, my JSON file looks like:
[
{
"F1": 2429546524130460000,
"F2": 2429519276857919500,
"F3.1.F1": 2428316170619109000,
"F3.1.F2": 0.0690932185744956,
"F3.1.F3": 2.6355498567408557,
"F3.1.F4": 0.4369495787854096,
...
"F3.10.F1": 2429415922764859400,
"F3.10.F2": 0.15328371980044203,
"F3.10.F3": 2.677944208300451,
"F3.10.F4": 0.31036472544281585,
"F4": 0.16889514829995647
},
... //repeated 2,509 times
{
"F1": 1143081876266241000,
"F2": 1143588785487818100,
"F3.1.F1": 1141377392726037800,
"F3.1.F2": 1.332366799133926,
"F3.1.F3": 0.24878185970548322,
"F3.1.F4": 1.560443994684636,
...
"F3.10.F1": "XXX",
"F3.10.F2": "XXX",
"F3.10.F3": "XXX",
"F3.10.F4": "XXX",
"F4": 2.2916768389567497
}
]
Clearly, making the necessary changes 2,511 times is impractical, so I am hoping there is a method to make the changes automatically. I can code, but I could not find any specific solutions anywhere to go from a CSV to a JSON with the JSON output matching a specific JSON schema. Preferably, I would like a solution that is not restricted to just converting this one set of data to this one specific format, i.e., a general solution that could be used with a different CSV and different JSON schema.

flatten a structure where the elements can be an object or a nested array

I have a JSON document that I would like to flatten where I get a resultant object resemling this:
[
{
"name": "Form/Field/Tokens.php",
"line": "62",
"severity": "info",
"message": "Expected #param annotation",
"source": "PhanCommentParamOutOfOrder"
}
]
In order to do this, I would need to flatten the file object but different ones represent the nested error node as an object or an array. I'm not sure how to flatten it.
Here's the given document.
{
"version": "6.5",
"file": [
{
"name": "Form/Field/Tokens.php",
"error": {
"line": "62",
"severity": "info",
"message": "Expected #param annotation",
"source": "PhanCommentParamOutOfOrder"
}
},
{
"name": "Console/Command/Reference.php",
"error": [
{
"line": "40",
"severity": "warning",
"message": "Possibly zero references to use statement",
"source": "PhanUnreferencedUseNormal"
},
{
"line": "54",
"severity": "warning",
"message": "Property has undeclared type",
"source": "PhanUndeclaredTypeProperty"
}
]
}
]
}
Given JQ's type builtin, you just need an if-then-else expression to determine if .file.error is an array or an object.
.file | map({name} + (.error |
if type == "array" then .[] else . end))
Online demo

TSQL Azure Modify JSON in array

I'm using SQL azure and storing JSON data in a varchar column. Can anyone advise how I can update a value stored in array? I would like to change status from Active to Disabled for example.
JSON Sample
{
"services": [
{
"attributes": {
"Status": "Active",
"Additional_Notes": ""
},
"type": "Type1",
"description": "a",
"reference": "312ce8e7-9913-4758-82af-10551d63920a"
},
{
"attributes": {
"ContractNo": "1234",
"Additional_Notes": ""
},
"type": "Type2",
"description": "b",
"reference": "962c7bc6-882c-47ee-b581-c5d3436d4f99"
},
{
"attributes": {
"ContractNo": "5678",
"Additional_Notes": "test note 123"
},
"type": "Type3",
"description": "b",
"reference": "86fc37ed-59d3-42c4-a0be-ca54bfdc0fec"
}
]
}
I currently use JSON_VALUE and JSON_QUERY to query the data which is fine, but not idea how to use JSON_MODIFY to edit the value. I know the reference but not sure how to get to the value.
Any advice would be much appreciated.
Ps. I'm stuck with the JSON in this format and cannot change it.
You need to use JSON_Modify to change JSON, for the given question, you can do like below
declare #json varchar(1000)='{
"services": [
{
"attributes": {
"Status": "Active",
"Additional_Notes": ""
},
"type": "Type1",
"description": "a",
"reference": "312ce8e7-9913-4758-82af-10551d63920a"
},
{
"attributes": {
"ContractNo": "1234",
"Additional_Notes": ""
},
"type": "Type2",
"description": "b",
"reference": "962c7bc6-882c-47ee-b581-c5d3436d4f99"
},
{
"attributes": {
"ContractNo": "5678",
"Additional_Notes": "test note 123"
},
"type": "Type3",
"description": "b",
"reference": "86fc37ed-59d3-42c4-a0be-ca54bfdc0fec"
}
]
}'
set #json= JSON_modify(#json,'$.services[0].attributes.Status','Any new value')
select #json
You will need to first understand what are the JSON functions available and what they can do.For this ,this link helped me:The Ultimate SQL Server JSON Cheat Sheet
Some more references to update multiple elements:
SQL Server JSON_Modify, How to Update all?

Mule json schema validation

I am trying to use Validate JSON schema component in Mule flow and I am getting com.fasterxml.jackson.core.JsonParseException for the json that is passed. Below is the json schema, json sample and code of Mule flow. Can you please point me where am I doing mistake?
Json schema:
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "object",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "integer"
},
"name": {
"description": "Name of the product",
"type": "string"
},
"price": {
"type": "number",
"minimum": 0,
"exclusiveMinimum": true
}
},
"required": ["id", "name", "price"]
}
Json passed to POST method:
[
{
"id": 2,
"name": "An ice sculpture",
"price": 12.50,
},
{
"id": 3,
"name": "A blue mouse",
"price": 25.50,
}
]
Error :
Root Exception stack trace:
com.fasterxml.jackson.core.JsonParseException: Unexpected character ('}' (code 125)): was expecting double-quote to start field name
at [Source: java.io.InputStreamReader#6e7f030; line: 6, column: 5]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:1419)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportError(ParserMinimalBase.java:508)
Mule Flow:
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="jsonschemavalidationFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/" allowedMethods="POST" doc:name="HTTP"/>
<json:validate-schema schemaLocation="jsonschema.json" doc:name="Validate JSON Schema"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
There are few errors in both the JSON and the Schema as follows:-
There is an extra comma, after "price": 12.50
So the Valid JSON will be :-
[
{
"id": 2,
"name": "An ice sculpture",
"price": 12.50
},
{
"id": 3,
"name": "A blue mouse",
"price": 25.50
}
]
In JSON Schema file jsonschema.json There are two errors:-
You need to put "type": "array" instead of "type": "object"
and next is "exclusiveMinimum": true seems to be invalid with this JSON input...
So, the correct working JSON schema will be :-
{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "Product",
"description": "A product from Acme's catalog",
"type": "array",
"properties": {
"id": {
"description": "The unique identifier for a product",
"type": "integer"
},
"name": {
"description": "Name of the product",
"type": "string"
},
"price": {
"type": "number",
"minimum": 0
}
},
"required": ["id", "name", "price"]
}
Try it.. it will work fine :)
The json u r passing is not proper. It has an extra comma ',' after price: 12.50. Even after second price element also an extra comma is added.
Just remove it and it works fine.

Swagger editor always gives error on json

For me not a single json string worked with the swagger editor.
Here is an example which i took from the page:
{
"swaggerVersion": "2.0",
"basePath": "http://localhost:8000/greetings",
"apis": [
{
"path": "/hello/{subject}",
"operations": [
{
"method": "GET",
"summary": "Greet our subject with hello!",
"type": "string",
"nickname": "helloSubject",
"parameters": [
{
"name": "subject",
"description": "The subject to be greeted.",
"required": true,
"type": "string",
"paramType": "path"
}
]
}
]
}
],
"models": {}
}
So current error for this, is:
✖ YAML Syntax Error
Missed comma between flow collection entries at line 2, column 14: "swagger: "2.0", ^
I don't know where did you take the base code for but that syntax seems to be totally wrong (at least for swagger 2), I recommend you to take a look to the official specification.
About the code you pasted, I refactorized it, try with this one:
{
"swagger": "2.0",
"host": "localhost:8000",
"basePath": "/greetings",
"info": {
"title": "Some title",
"version": "0.0.1"
},
"paths": {
"/hello/{subject}": {
"get": {
"summary": "Greet our subject with hello!",
"parameters": [{
"name": "subject",
"description": "The subject to be greeted.",
"required": true,
"type": "string",
"in": "path"
}],
"responses": {
"default": {
"description": "Some description",
"schema": {
"type": "string"
}
}
}
}
}
}
}
Even though the OP's syntax was wrong, this is for other people who are searching for an answer and their syntax is correct.
If you are getting the JSON data from the browser and you have a browser extension that formats and displays nicely the JSON data, it might cause trouble (because of the formatting) when pasting in Swagger Editor.
If that's the case, try disabling the extension or go incognito.