-1

I'm a novice in JavaScript world, so please be patient.

I've got some raw "JSON" response with duplicate keys and I want to make it more readable. The problem here is in embedded parsing: when I'm trying to pass it through the JSON.stringify - it parses my input to JSON object and duplicate keys disappear.

How can I approach this problem in other way and preserve duplicates?

Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Developer87
  • 2,448
  • 4
  • 23
  • 43
  • 3
    What do you want to happen with the duplicate entries? Can you give a (representative) example of your JSON and show the expected output that should be produced? – David Thomas Sep 13 '18 at 09:43
  • Duplicate keys aren't valid in JSON, how would you want to make it more readable? – Luca Kiebel Sep 13 '18 at 09:44
  • [Related question](https://stackoverflow.com/questions/30842675/how-to-get-the-json-with-duplicate-keys-completely-in-javascript). – Ram Sep 13 '18 at 09:44
  • If i understand it correctly, you could enclose objects with same key names inside an array? – Advena Sep 13 '18 at 09:47
  • Related: [Does JSON syntax allow duplicate keys in an object?](https://stackoverflow.com/questions/21832701/does-json-syntax-allow-duplicate-keys-in-an-object) – str Sep 13 '18 at 09:48

1 Answers1

2

is super exercice for practicing Regex parsing.

lets take look of my code step by step :

// Create inline json with nested object.
const originalJson = `{dfkoz:'efzf','dd':"ddd",'zfzfzf','foo': {bar:1}}`;

then let split it in array by expected lines.

const lines = originalJson.replace(/[^,],+/g,"$&\n") \\ after each ',' let add '\n' after.
.replace(/{/g,"$&\n") // Add \n after any '{'
.replace(/}/g,"\n$&") // Add \n before any '}'
.split('\n'); // Split string to array with breakline separator

At this point you will have array like this :

0: "{"
1: "dfkoz:'efzf',"
2: "'dd':"ddd","
3: "'zfzfzf',"
4: "'foo': {"
5: "bar:1"
6: "}"
7: "}"

then you have to loop on it and add your tab and break line logic :

let formatedJson = '';
let nbrOfIndent = 0;
let previousNbrOfIndent = 0;
let isFindIndentMarker = false;
lines.forEach(line => {
  previousNbrOfIndent = nbrOfIndent;
  // if line is just { or finish by {, we have to increment number of \t for next loop iteration.
  if(line === '{' || line.substr(-1) === '{') {
    nbrOfIndent++;
    isFindIndentMarker = true;
  }
  // if current line is just } or finish by , we have to decrease number of \t for next tick.
  else if(line === '}' || line.substr(-1) !== ',') {
    nbrOfIndent--;
    isFindIndentMarker = true;
  }
  formatedJson += "\t".repeat((!isFindIndentMarker)?nbrOfIndent:previousNbrOfIndent) + line + "\n";
});

Online Sample

Yanis-git
  • 7,737
  • 3
  • 24
  • 41
  • `originalJson` is not JSON at all. – str Sep 13 '18 at 10:13
  • So "*without actual parsing*" means "*writing your own parser with regex*" to you? – Bergi Sep 13 '18 at 10:14
  • didn't find duplicates in your originalJson text – Developer87 Sep 13 '18 at 10:16
  • yes that suggest me this approch. As @str said, is not valid json, so for debuging pruposal, my code can do the job to point where is actual issue to make it valid. @Developer@87, you can modify my code to add some duplicate key. My code is just "beautifier unvalid json" – Yanis-git Sep 13 '18 at 10:16