juiceb0xk
juiceb0xk

Reputation: 969

Formatting my json

I have developed a Python script to append json data to one another, in hopes of creating multiple json objects. However, doing so does not format it correctly and I'm unsure as to how I should be formatting my json to create multiple json objects.

{"name": "8808", "ip": "192.168.241.110", "cameras": {"front": ["nf091"], "inside": ["nf067"], "right": ["004317"], "rear": ["000189"], "left": ["nf084"]}, "serial":     "000002", "simId": 197078302}
{"name": "8893", "ip": "192.168.241.137", "cameras": {"front": ["nf052"], "inside": ["000211"], "right": ["000069"], "rear": ["000441"], "left": ["000400"]}, "serial":     "000277", "simId": 197057802}
{"name": "1620", "ip": "192.168.242.145", "cameras": {"front": ["000174"], "inside": ["000197"], "right": ["000304"], "rear": ["000295"], "left": ["000553"]}, "serial":     "000084", "simId": 310922501}
{"name": "0632", "ip": "192.168.242.166", "cameras": {"front": ["nf050"], "inside": ["nf022"], "right": ["nf047"], "rear": ["ne056"], "left": ["ne083"]}, "serial":     "NF016", "simId": 310897301}
{"name": "1544", "ip": "192.168.242.234", "cameras": {"front": ["000061"], "inside": ["000068"], "right": ["004440"], "rear": ["000219"], "left": ["005516"]}, "serial":     "000200", "simId": 310839901}
{"name": "12HA", "ip": "192.168.243.116", "cameras": {"front": ["000625"], "right": ["nf104"], "rear": ["ne047"], "left": ["000717"]}, "serial": "000181", "simId":     510339201}

Copying this data into a json validator gives me errors whereas it's expecting an EOF. However, how can I modify the above data to create multiple json objects and hence forth validate my json?

Upvotes: 0

Views: 75

Answers (3)

Fred Gandt
Fred Gandt

Reputation: 4312

JSON is always only one object, beit an array or literal object, it cannot be a bunch of separate arrays or literal objects.

JSON is built on two structures:
* A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
* An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.

Assuming your posted set of objects are supposed to be one JSON, you can make each object an element of an array, and pass it around like that.

The example below uses JavaScript, but hopefully you'll get the idea.

const arr = [
    {"name": "8808", "ip": "192.168.241.110", "cameras": {"front": ["nf091"], "inside": ["nf067"], "right": ["004317"], "rear": ["000189"], "left": ["nf084"]}, "serial": "000002", "simId": 197078302},
    {"name": "8893", "ip": "192.168.241.137", "cameras": {"front": ["nf052"], "inside": ["000211"], "right": ["000069"], "rear": ["000441"], "left": ["000400"]}, "serial": "000277", "simId": 197057802},
    {"name": "1620", "ip": "192.168.242.145", "cameras": {"front": ["000174"], "inside": ["000197"], "right": ["000304"], "rear": ["000295"], "left": ["000553"]}, "serial": "000084", "simId": 310922501},
    {"name": "0632", "ip": "192.168.242.166", "cameras": {"front": ["nf050"], "inside": ["nf022"], "right": ["nf047"], "rear": ["ne056"], "left": ["ne083"]}, "serial": "NF016", "simId": 310897301},
    {"name": "1544", "ip": "192.168.242.234", "cameras": {"front": ["000061"], "inside": ["000068"], "right": ["004440"], "rear": ["000219"], "left": ["005516"]}, "serial": "000200", "simId": 310839901},
    {"name": "12HA", "ip": "192.168.243.116", "cameras": {"front": ["000625"], "right": ["nf104"], "rear": ["ne047"], "left": ["000717"]}, "serial": "000181", "simId": 510339201}
];

var packed_json = JSON.stringify( arr ), // transportable
    json = JSON.parse( packed_json ); // operable

console.log( json[ 1 ].simId ); // for example

Upvotes: 2

Rohìt Jíndal
Rohìt Jíndal

Reputation: 27232

Some observations :

  • You have to wrapped all the objects into an single array seperated by (,) as objects will behave like an array elements.
  • You can iterate the array using ES6 for...of loop or using ES6 map() method.

Valid JSON :

[{"name": "8808", "ip": "192.168.241.110", "cameras": {"front": ["nf091"], "inside": ["nf067"], "right": ["004317"], "rear": ["000189"], "left": ["nf084"]}, "serial":     "000002", "simId": 197078302},
{"name": "8893", "ip": "192.168.241.137", "cameras": {"front": ["nf052"], "inside": ["000211"], "right": ["000069"], "rear": ["000441"], "left": ["000400"]}, "serial":     "000277", "simId": 197057802},
{"name": "1620", "ip": "192.168.242.145", "cameras": {"front": ["000174"], "inside": ["000197"], "right": ["000304"], "rear": ["000295"], "left": ["000553"]}, "serial":     "000084", "simId": 310922501},
{"name": "0632", "ip": "192.168.242.166", "cameras": {"front": ["nf050"], "inside": ["nf022"], "right": ["nf047"], "rear": ["ne056"], "left": ["ne083"]}, "serial":     "NF016", "simId": 310897301},
{"name": "1544", "ip": "192.168.242.234", "cameras": {"front": ["000061"], "inside": ["000068"], "right": ["004440"], "rear": ["000219"], "left": ["005516"]}, "serial":     "000200", "simId": 310839901},
{"name": "12HA", "ip": "192.168.243.116", "cameras": {"front": ["000625"], "right": ["nf104"], "rear": ["ne047"], "left": ["000717"]}, "serial": "000181", "simId":     510339201}]

enter image description here

Upvotes: 1

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72825

You should probably put all these together into an outer container like a list. Something like [{...}, {...}, {...}].

Upvotes: 1

Related Questions