I would like to be able to parse a JSON file that is structured something like:
[
{
"id": 1,
"name": {
"first": "name",
"secound": "name",
"last": "name"
},
"skills": [
"python",
"javascript"
],
"otherInfo": {
"something": 45,
"something2": 49
}
},
{
"id": 2,
"name": {
"first": "name",
"secound": "name",
"last": "name"
},
"skills": [
"python",
"javascript"
],
"otherInfo": {
"something": 45,
"something2": 49
}
}
etc...
]
into something looking like
array(
[0] => array(
0 => "id"
1 => "firstname"
2 => "otherData"
),
[1] => array(
0 => "id"
1 => "firstname"
2 => "otherData"
),
etc...
)
I'm pretty sure i have an idea on how to convert it into the format I want, but im having trouble actually reading the data from the file.
The two major issues im having:
Reading it from inside the jar.
Most of the examples used json.simple, and the json library i'm using doesn't seem to have that.
I tried some examples online, and a couple of the answers on this post
but no luck the biggest issue is that all of them are giving examples for reading an external file, while I'm trying to read one that is packaged inside the jar.
My project tree:
MyProject
L src
L myPackage
L MyClass.java
L MyJsonFile.json
The closest thing that I'm guessing almost worked is this (from the link above):
import org.json.JSONArray;
//code
JSONArray myJSONArray = new JSONArray(Main.class.getResourceAsStream("myFile.json"));
But that only seems to throw an error:
org.json.JSONException: JSONArray initial value should be a string or collection or array.
Thanks in advance!
The problem is org.json.JSONArray will only accept String,Collection or Array but you are trying to pass the InputStream object. which is what error messages also says
org.json.JSONException: JSONArray initial value should be a string or collection or array
So first convert the InputStream into String
InputStream inputStream = Main.class.getResourceAsStream("myFile.json");
InputStreamReader isReader = new InputStreamReader(inputStream);
BufferedReader reader = new BufferedReader(isReader);
StringBuffer sb = new StringBuffer();
String str;
while((str = reader.readLine())!= null){
sb.append(str);
}
And then convert it into JSONArray
JSONArray myJSONArray = new JSONArray(sb.toString());
After here you can iterate the JSONArray using for loop, like here
I'm facing difficulties in a scenario that I need to read a JSON object, in Java, that has no double quotes in the keys and no values, like the example below:
"{id: 267107086801, productCode: 02-671070868, lastUpdate: 2018-07-15, lastUpdateTimestamp: 2018-07-15 01:49:58, user: {pf: {document: 123456789, name: Luis Fernando}, address: {street: Rua Pref. Josu00e9 Alves Lima,number:37}, payment: [{sequential: 1, id: CREDIT_CARD, value: 188, installments: 9}]}"
I was able to add the double quotes in the fields using the code below, with replaceAll and the Gson library:
String jsonString = gson.toJson (obj);
String jsonString = jsonString.replaceAll ("([\\ w] +) [] *:", "\" $ 1 \ ":"); // to quote before: value
jsonString = jsonString.replaceAll (": [] * ([\\ w # \\.] +)", ": \" $ 1 \ ""); // to quote after: value, add special character as needed to the exclusion list in regex
jsonString = jsonString.replaceAll (": [] * \" ([\\ d] +) \ "", ": $ 1"); // to un-quote decimal value
jsonString = jsonString.replaceAll ("\" true \ "", "true"); // to un-quote boolean
jsonString = jsonString.replaceAll ("\" false \ "", "false"); // to un-quote boolean
However, fields with dates are being broken down erroneously, for example:
"{"id" : 267107086801,"productCode" : 02-671070868,"lastUpdate" : 2018-07-15,"lastUpdateTimestamp" : 2018-07-15 "01" : 49 : 58,"user" :{"pf":{"document" : 123456789, "name" : "Luis" Fernando},"address" :{"street" : "Rua"Pref.Josu00e9AlvesLima,"number" : 37},"payment" : [{"sequential" : 1,"id" : "CREDIT_CARD","value" : 188,"installments" : 9}]}"
Also, strings with spaces are wrong as well. How could I correct this logic? What am I doing wrong? Thanks in advance.
String incorrectJson = "{id: 267107086801, productCode: 02-671070868,"
+ " lastUpdate: 2018-07-15, lastUpdateTimestamp: 2018-07-15 01:49:58,"
+ " user: {pf: {document: 123456789, name: Luis Fernando},"
+ " address: {street: Rua Pref. Josu00e9 Alves Lima,number:37},"
+ " payment: [{sequential: 1, id: CREDIT_CARD, value: 188, installments: 9}]}";
String correctJson = incorrectJson.replaceAll("(?<=: ?)(?![ \\{\\[])(.+?)(?=,|})", "\"$1\"");
System.out.println(correctJson);
Output:
{id: "267107086801", productCode: "02-671070868", lastUpdate:
"2018-07-15", lastUpdateTimestamp: "2018-07-15 01:49:58", user: {pf:
{document: "123456789", name: "Luis Fernando"}, address: {street: "Rua
Pref. Josu00e9 Alves Lima",number:"37"}, payment: [{sequential: "1",
id: "CREDIT_CARD", value: "188", installments: "9"}]}
One downside of non-trivial regular expressions is they can be hard to read. The one I use here matches each literal value (but not values that are objects or arrays). I am using colons, commas and curly braces to guide the matching so I don’t need to care what is inside each string value, it may be any characters (except comma or right curly brace). The parts mean:
(?<=: ?): there’s a colon an optionally a blank before the value (lookbehind)
(?![ \\{\\[]) the value does not start with a blank, curly brace or square bracket (negative lookahead; blank because we don’t want a blank between the colon and the value to be taken as part of the value)
(.+?): the value consists of at least one character, as few as possible (reluctant quantifier; or regex would try to take the rest of the string)
(?=,|}): after the value comes either a comma or a right curly brace (positive lookahead).
Without being well versed in JSON I don’t think you need to quote the name. You may, though:
String correctJson = incorrectJson.replaceAll(
"(?<=\\{|, ?)([a-zA-Z]+?): ?(?![ \\{\\[])(.+?)(?=,|})", "\"$1\": \"$2\"");
{"id": "267107086801", "productCode": "02-671070868", "lastUpdate":
"2018-07-15", "lastUpdateTimestamp": "2018-07-15 01:49:58", user: {pf:
{"document": "123456789", "name": "Luis Fernando"}, address:
{"street": "Rua Pref. Josu00e9 Alves Lima","number": "37"}, payment:
[{"sequential": "1", "id": "CREDIT_CARD", "value": "188",
"installments": "9"}]}
The following code takes care single quote present in JSON string as well as a key containing number
jsonString = jsonString.replaceAll(" :",":"); // to trip space after key
jsonString = jsonString.replaceAll(": ,",":,");
jsonString = jsonString.replaceAll("(?<=: ?)(?![ \{\[])(.+?)(?=,|})", ""$1"");
jsonString = jsonString.replaceAll("(?<=\{|, ?)([a-zA-Z0-9]+?)(?=:)",""$1"");
jsonString = jsonString.replaceAll(""true"", "true"); // to un-quote boolean
jsonString = jsonString.replaceAll(""false"", "false"); // to un-quote boolean
jsonString = jsonString.replaceAll(""null"", "null");// to un-quote null
jsonString = jsonString.replaceAll(":",", ":"" ,"); // to remove unnecessary double quotes
jsonString = jsonString.replaceAll("true"", "true"); // to un-quote boolean
jsonString = jsonString.replaceAll("'",", "',"); // to handle single quote within json string
jsonString = jsonString.replaceAll("'},", "'}","); // to put double quote after string ending with single quote
I have written a few rest services to extract data from a mysql database and display it in json format on Postman client. However for some of the response items, I get this unwanted \\r character in the response values. For example:
{
"colour": "yellow",
"deliveryCharge": 5,
"description": "Mangoes from Ratnagiri",
"keyFeatures": [
"Seedless\\\r",
"Ripe and Sweet"
],
"price": 100,
"productId": 49,
"productName": "Alfonso",
"specifications": {
"entry": [
{
"key": "Feature",
"value": "N/A"
}
]
},
"stock": 20,
"warranty": 1
}
How do I get rid of these characters in the final json response? I have tried isolating them using .replace() and .split() methods for the output strings fetched from the database, but it doesn't work.
You have two escaped character \ and \r in the string. trim method in String class can remove \r but not \. If you don't want to send these character, You will have to do it yourself before serializing the objec to to JSON.
I have the following entry in MarkLogic in JSON format:
{
"identifier":"user1",
"attributesList": [
{
"firstName": "James",
"address_1": "Farcity"
}
]
}
If, I'm going to query that using the below format:
{
$query:
{
"identifier":"user1",
"attributesList": [
{
"firstName": "James"
}
]
}
}
this will match and return back the expected result with a count of 1 because "firstName" is equal to "James".
However, if I do the following:
{
$query:
{
"identifier":"user1",
"attributesList": [
{
"address_1": "Farcity"
}
]
}
}
it will not give back any result even if "address_1" exactly matches which is "Farcity". I already tried this on other JSON key as well, it works fine with all except those with underscores in the key.. Is this a reserved character? If so, is there a way to escape this so that the key "address_1" or "county_state" can still be matched?
It looks like the Json object converts the underscores to a double underscore
running this:
xquery version "1.0-ml";
import module namespace json="http://marklogic.com/xdmp/json"
at "/MarkLogic/json/json.xqy";
let $j :=
'{
"identifier":"user1",
"attributesList": [
{
"firstName": "James",
"address_1": "Farcity"
}
]
}'
return
json:transform-from-json( $j)
you will get this out put
<json type="object" xmlns="http://marklogic.com/xdmp/json/basic">
<identifier type="string">user1</identifier>
<attributesList type="array">
<json type="object">
<firstName type="string">James</firstName>
<address__1 type="string">Farcity</address__1>
</json>
</attributesList>
</json>
So try querying with a double underscores. Also If you are using Marklogic 6 or 7 it converts the Json to xml. so you could just try to query by example using the XML format.
See http://docs.marklogic.com/xdmp:encode-for-NCName for the exact algorithm and function used to map JSON field names to QNames
I am creating an irc client in Java. It work fine but the message from the server is a bit "messed-up"
for example :User1!webirc#1.9.com PRIVMSG #channel :test. So i wanna know how to parse the irc message to human readable? Here is a regex that i found ^(:(\\S+) )?(\\S+)( (?!:)(.+?))?( :(.+))?$ for irc message.
The IRC Protocol is documented here: https://www.rfc-editor.org/rfc/rfc2812
2.3.1 Message format in Augmented BNF
The protocol messages must be extracted from the contiguous stream of
octets. The current solution is to designate two characters, CR and
LF, as message separators. Empty messages are silently ignored,
which permits use of the sequence CR-LF between messages without
extra problems.
The extracted message is parsed into the components ,
and list of parameters ().
The Augmented BNF representation for this is:
message = [ ":" prefix SPACE ] command [ params ] crlf
prefix = servername / ( nickname [ [ "!" user ] "#" host ] )
command = 1*letter / 3digit
params = *14( SPACE middle ) [ SPACE ":" trailing ]
=/ 14( SPACE middle ) [ SPACE [ ":" ] trailing ]
nospcrlfcl = %x01-09 / %x0B-0C / %x0E-1F / %x21-39 / %x3B-FF
; any octet except NUL, CR, LF, " " and ":"
middle = nospcrlfcl *( ":" / nospcrlfcl )
trailing = *( ":" / " " / nospcrlfcl )
SPACE = %x20 ; space character
crlf = %x0D %x0A ; "carriage return" "linefeed"