Giter Club home page Giter Club logo

flatten-serde-json's Introduction

Flatten serde Json

This crate flatten serde_json Object in a format similar to elastic search.

That's used in production in meilisearch

Examples

There is nothing to do

{
  "id": "287947",
  "title": "Shazam!",
  "release_date": 1553299200,
  "genres": [
    "Action",
    "Comedy",
    "Fantasy"
  ]
}

Flattens to:

{
  "id": "287947",
  "title": "Shazam!",
  "release_date": 1553299200,
  "genres": [
    "Action",
    "Comedy",
    "Fantasy"
  ]
}

Objects

{
  "a": {
    "b": "c",
    "d": "e",
    "f": "g"
  }
}

Flattens to:

{
  "a.b": "c",
  "a.d": "e",
  "a.f": "g"
}

Array of objects

{
  "a": [
    { "b": "c" },
    { "b": "d" },
    { "b": "e" },
  ]
}

Flattens to:

{
  "a.b": ["c", "d", "e"],
}

Array of objects with normal value in the array

{
  "a": [
    42,
    { "b": "c" },
    { "b": "d" },
    { "b": "e" },
  ]
}

Flattens to:

{
  "a": 42,
  "a.b": ["c", "d", "e"],
}

Array of objects of array of objects of ...

{
  "a": [
    "b",
    ["c", "d"],
    { "e": ["f", "g"] },
    [
        { "h": "i" },
        { "e": ["j", { "z": "y" }] },
    ],
    ["l"],
    "m",
  ]
}

Flattens to:

{
  "a": ["b", "c", "d", "l", "m"],
  "a.e": ["f", "g", "j"],
  "a.h": "i",
  "a.e.z": "y",
}

Collision between a generated field name and an already existing field

{
  "a": {
    "b": "c",
  },
  "a.b": "d",
}

Flattens to:

{
  "a.b": ["c", "d"],
}

flatten-serde-json's People

Contributors

irevoire avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

romilpunetha

flatten-serde-json's Issues

Flatten arrays further

Currently, the array flattener merges the arrays.

{ "a" : [ "b", [ "c", "d" ]] } => { "a" : ["b", "c", "d"]}

Could we add a parameter that completely flattens the json including the arrays?

{ "a" : [ "b", [ "c", "d" ]] } => { "a.0" : "b", "a.1.0": "c", "a.1.1" : d}

The idea is to add another parameter to the flatten function to take an optional field that denotes if the arrays need to be flattened.

pub fn flatten(json: &Map<String, Value>, flatten_array: Option<bool>) -> Map<String, Value>

This would result in the following:

{
"a": [
            "b",
            ["c", "d"],
            { "e": ["f", "g"] },
            [
                { "h": "i" },
                { "e": ["j", { "z": "y" }] } 
            ],
            ["l"],
            "m"
         ]
}

=> 

{
  "a.0": "b",
  "a.1.0": "c",
  "a.1.1": "d",
  "a.2.e.0": "f",
  "a.2.e.1": "g",
  "a.3.0.h": "i",
  "a.3.1.e.0": "j",
  "a.3.1.e.1.z": "y",
  "a.4.0": "l",
  "a.5": "m"
}
            

Would you be willing to accept a PR for this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.