How to change JSON file to be readable? - json

I've created a JSON.
This JSON file will be a source of query in ElasticSearch
This query works in ElasticSearch and returns results.
When I store the query to JSON, I see that JSON is valid. But when I read it from JSON I have an error.
It's my JSON:
{
"function_score": {
"query": {
"bool": {
"should": [
{
"query_string": {
"default_field": "headline",
"query": "engineer"
}
},
{
"match_all": {}
}
]
}
},
"functions": [
{
"script_score": {
"script": {
"params": {
"queryVector": [1,2,5],
"max_score": "max_score"
},
"source": "if (_score>0 && params.max_score>0){return doc['embedding_headline'].size() == 0 ? 0 : Math.min(Math.max(_score/params.max_score,cosineSimilarity(params.queryVector, 'embedding_headline')),1) + 1.0} else { return doc['embedding_headline'].size() == 0 ? 0 : cosineSimilarity(params.queryVector, 'embedding_headline') + 1.0}",
"lang": "painless"
}
}
}
],
"score_mode": "max",
"boost_mode": "replace",
}
}
I use python for reading JSON.
It's code for reading:
import json
file_path = 'queries.json'
with open(file_path) as f:
data = json.load(f)
print(data)
It's error:
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 34 column 5 (char 1012)
What should I change in this JSON to make it readable?
Thanks

That json is invalid.
I just pasted it into a json validator and it has errors in it.
CHeck this link:
json lint
Paste your json there, it throws the following errors:
Error: Parse error on line 30:
..._mode": "replace", }}
----------------------^
Expecting 'STRING', got '}'
If you are using python it often throws errors due to string quotation as well.
Try to enclose the script source field in single quotes instead of doubles. That could be another problem.
Last but not the least, function score queries are extremely slow, and I would recommend not using them!

Related

Nest a Json in Speech Marks

I have a JSON that I'd like to nest into another JSON. Is this possible? Problem I'm facing is when I add Json #2 to #1 is gives me a formatting error.
JSON #1:
{"home":"This a sentence about home", "json":"<ADD JSON HERE>"}
JSON #2:
{
"homepage": {
"heading": [
{"h1":"Sentence for H1!"},
{"description":"Description about us"},
{"button1":"Learn More"},
],
}
What I've tried:
{"home":"This a sentence about home", "json":" { "homepage": {"heading": [ {"h1":"Sentence for H1!"},{"description":"Description about us"},{"button1":"Learn More"},],}"}
(If you are using JavaScript)
If you pass JSON to JSON.stringify it will escape your JSON.
You can then insert the escaped JSON in to your original JSON.
So:
const jsonToInsert = {
"homepage": {
"heading": [
{"h1":"Sentence for H1!"},
{"description":"Description about us"},
{"button1":"Learn More"}
]
}
};
const stringifiedJson = JSON.stringify(jsonToInsert);
const completeJson = {"home":"This a sentence about home", "json": stringifiedJson}
Should work - sorry if there’s typos, replying on my phone.
The second JSON isn't valid (extra commas and missing }), but this is:
{
"homepage": {
"heading": [
{"h1":"Sentence for H1!"},
{"description":"Description about us"},
{"button1":"Learn More"}
]
}
}
To embed it as a string, backslash-escape the embedded double-quotes:
{
"home": "This a sentence about home",
"json": "{\"homepage\": {\"heading\": [{\"h1\": \"Sentence for H1!\"}, {\"description\": \"Description about us\"}, {\"button1\": \"Learn More\"}]}}"
}

Parsing and cleaning text file in Python?

I have a text file which contains raw data. I want to parse that data and clean it so that it can be used further.The following is the rawdata.
"{\x0A \x22identifier\x22: {\x0A \x22company_code\x22: \x22TSC\x22,\x0A \x22product_type\x22: \x22airtime-ctg\x22,\x0A \x22host_type\x22: \x22android\x22\x0A },\x0A \x22id\x22: {\x0A \x22type\x22: \x22guest\x22,\x0A \x22group\x22: \x22guest\x22,\x0A \x22uuid\x22: \x221a0d4d6e-0c00-11e7-a16f-0242ac110002\x22,\x0A \x22device_id\x22: \x22423e49efa4b8b013\x22\x0A },\x0A \x22stats\x22: [\x0A {\x0A \x22timestamp\x22: \x222017-03-22T03:21:11+0000\x22,\x0A \x22software_id\x22: \x22A-ACTG\x22,\x0A \x22action_id\x22: \x22open_app\x22,\x0A \x22values\x22: {\x0A \x22device_id\x22: \x22423e49efa4b8b013\x22,\x0A \x22language\x22: \x22en\x22\x0A }\x0A }\x0A ]\x0A}"
I want to remove all the hexadecimal characters,I tried parsing the data and storing in an array and cleaning it using re.sub() but it gives the same data.
for line in f:
new_data = re.sub(r'[^\x00-\x7f],\x22',r'', line)
data.append(new_data)
\x0A is the hex code for newline. After s = <your json string>, print(s) gives
>>> print(s)
{
"identifier": {
"company_code": "TSC",
"product_type": "airtime-ctg",
"host_type": "android"
},
"id": {
"type": "guest",
"group": "guest",
"uuid": "1a0d4d6e-0c00-11e7-a16f-0242ac110002",
"device_id": "423e49efa4b8b013"
},
"stats": [
{
"timestamp": "2017-03-22T03:21:11+0000",
"software_id": "A-ACTG",
"action_id": "open_app",
"values": {
"device_id": "423e49efa4b8b013",
"language": "en"
}
}
]
}
You should parse this with the json module load (from file) or loads (from string) functions. You will get a dict with 2 dicts and a list with a dict.

indexing json file using solr

I am able to index a simple JSON using solr but for complex JSON which are having nested structures like below I am getting an error. I am using the curl command to index the JSON file using solr:
curl 'https://localhost:8983/solr/json_collection/update?commit=true' --data-binary #/home/mic.json -H 'Content-type:application/json'
Error:
Error - {"responseHeader":{"status":400,"QTime":12},"error":{"metadata":["error-class","org.apache.solr.common.SolrException"],"msg":"Error parsing JSON field value. Unexpected OBJECT_START","code":400}}
JSON:
[
{
"PART I, ITEM 1. BUSINESS": {
"GENERAL": {
"Our vision": {
"text": [
"Microsoft world."
]
},
"The ambitions that drive us": {
"text": [
"To carry ambitions:",
"* Create more personal computing."
],
"Create more personal computing": {
"text": [
"We strive available. website."
]
}
}
},
"ITEM 1A. RISK FACTORS": "Our opk."
}
}
]
Error
JSON
Your JSON seems to be erroneous. In either of the cases, single object or array of JSON, your JSON should follow basic conventions.
In case of single object, the syntax should be-
{ "key":"value"}
In case of Array of JSON, the syntax can be-
{
"key1":["value1", "value2",...],
"key2":["value12", "value22",...]
}

Cannot parse config file using grunt command for execution

I'm getting an error while working with grunt,
Error cannot parse config file unexpected token in default.json
I tried correcting the code but I'm receiving the same error
This is my default.json file:
{
"Amazon" : {
"lambda-arn" : "arn:aws:lambda:us-east-1:286506727102:function:myPandora",
},
"Pb": {
"url": "https://aiaas.xxx",
"app_id": "1xxxx1",
"user_key": "070xxxxxx",
"botname": "alexa"
},
}
Please help me with this error, Thank you.
Delete the commas at the end of lines 3 and 10 in your JSON to make it valid. E.g.
{
"Amazon": {
"lambda-arn": "arn:aws:lambda:us-east-1:286506727102:function:myPandora"
},
"Pb": {
"url": "https://aiaas.xxx",
"app_id": "1xxxx1",
"user_key": "070xxxxxx",
"botname": "alexa"
}
}

JSON Parsing Error

I got problem. I have this JSON automatically generated by Open Flash Chart php library. The problem is, OFC report JSON Parse Error [Syntax Error] while test result using http://www.jsonlint.com/ report that my JSON is fine. But, w3c parser report error too:(
Any help?
Here's the JSON:
{
"title": "Followers Trend",
"elements": [
{
"type": "area_hollow",
"fill-alpha": 0.35,
"values": [
],
"colour": "#5B56B6",
"text": "Followers",
"font-size": 12
}
],
"x_axis": {
"colour": "#A2ACBA",
"grid-colour": "#D7E4A3",
"offset": false,
"steps": 4,
"labels": {
"steps": 2,
"rotate": "vertical",
"colour": "#A2ACBA",
"labels": [
]
}
},
"x_legend": {
"text": "Week Trend (2009-08-17 - 2009-08-24)",
"style": "{font-size: 20px; color: #778877}"
},
"y_axis": {
"min": 0,
"max": 150,
"steps": 30
}
}
A few things I learned while playing with JSON is:
If you have validate the JSON on various JSON validation services and the result is GOOD. But, when you failed to eval it, try to wrap your JSON using ( and ) => ({jsondata})
var json = eval( "(" + jsonString + ")" );
NEVER build the JSON yourself. It's a gate to failure. Always use official or popular JSON library (depending on your language). For example:
On PHP: use json_encode()
On Java Android: use org.json.JSONObject
A list of all other available library to play with JSON is listed in JSON official page.
To display and format JSON data, you can use JSONViewer.
I think the w3c parser is having issues, I couldn't even get it to parse this:
{
"title" : "Followers Trend"
}
It gave me this error:
Validation errors:
lexer couldn't parse at "{
"title" : "Followers Trend"
}"
http://json.bloople.net helps you visualise the code to find and correct errors.
try this code, JSON.parse() method is not able to handle string which is in a
single quote as a value in the right-hand side. also if you want to handle the
UTF-8 character code, then it will do.
parseJSON = function() {
var data = {};
var reader = new FileReader();
reader.onload = function() {
try {
data = JSON.parse(reader.result.replace(/'/g, "\""));
console.log(data)
} catch (ex) {
console.log('error' + ex);
}
};
reader.readAsText(fileSelector_test[0].files[0], 'utf-8');
}